android.widget.LinearLayout无法在RecyclerView中强制转换为android.widget.TextView?

时间:2015-02-09 10:36:07

标签: java android android-recyclerview

我正在尝试使用新的Recycler View列出数据。我跟着这个tutorial&现在面临着一个问题。

这是我的

MainActivity.java

package com.example.i.newworkspace;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView tv;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
String[] myDataset = {"One","Two","Three","Four","Five"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    mAdapter = new MyAdapter(myDataset);
    recyclerView.setAdapter(mAdapter);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}
 class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

{
    String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        ViewHolder vh = new ViewHolder((TextView) v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.mTextView.setText(mDataset[position]);

    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }

    MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView

        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

my_text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

logcat的:

 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                at com.example.i.newworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:78)
                at com.example.i.nuewworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:59)

第78行:

        ViewHolder vh = new ViewHolder((TextView) v);

3 个答案:

答案 0 :(得分:7)

您应该注意到 my_text_view.xml 是一个LinearLayout所以:

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        // Find the TextView in the LinearLayout
        mTextView = v.findViewById(R.id.textView));
    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // Give the view as it is
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

答案 1 :(得分:2)

膨胀的视图也包含根元素,因此你需要通过findviewbyid函数创建textview,然后将其传递给ViewHolder构造函数

@Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        TextView tv=(TextView)v.findViewById(R.id.textView);
        ViewHolder vh = new ViewHolder(tv);
        return vh;
    }

祝你好运

答案 2 :(得分:1)

您正在尝试将LinearLayout强制转换为TextView。

更改此行:

ViewHolder vh = new ViewHolder((TextView) v);

为:

ViewHolder vh = new ViewHolder((TextView) v.findViewById(R.id.textView));