线性布局不能转换为文本视图:教程创建列表和卡片



我正在尝试按照Android官方文档创建列表和卡片。

我在这里发现有必要在 TextView 中投射视图持有人,但是当我这样做时,我遇到以下错误:java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

我的适配器

public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
    private Game[] mDataset;
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.textViewName);
        }
    }
    // Provide a suitable constructor (depends on the kind of dataset)
    public CardsViewAdapter(Game[] myDataset) {
        mDataset = myDataset;
    }
    // Create new views (invoked by the layout manager)
    @Override
    public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cards_resume_game, parent, false);
        // set the view's size, margins, paddings and layout parameters
        //...
        ViewHolder vh = new ViewHolder((TextView) v);
        return vh;
    }
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position].getId_game());
    }
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

我的自定义视图 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:tag="cards main container">
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/blue_50"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="5dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

你把这两个混为一谈了。假设在

// create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cards_resume_game, parent, false);

R.layout.cards_resume_gameLinearLayout,然后你应该有:

ViewHolder vh = new ViewHolder(v);

并让视图持有者构造函数接受视图:

public ViewHolder(View v) {
    mTextView = (TextView) v.findViewById(R.id.some_text_view_id);
}

其中some_text_view_id是所述布局中的文本视图,将由findViewById()获取

在您的适配器中,您将 TextView 投射到 ViewHolder,这是不必要的,并且您还以冗余方式调用 ViewHolder。试试这个:

return new ViewHolder(v)而不是ViewHolder vh = new ViewHolder((TextView)

不需要此强制转换的原因是,您已经在 ViewHolder 构造函数下强制转换mView,或者更确切地说,您已经在定义它应该期望的视图。

最新更新