我正在尝试遵循Creating Lists and Cards.的Android官方文档

我发现here有必要在TextView的ViewHolder中进行强制转换,但是当我这样做时,出现以下错误: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>

最佳答案

在您的适配器中,您不需要将TextView强制转换为ViewHolder,并且您还以冗余方式调用ViewHolder。尝试这个:

return new ViewHolder(v)代替ViewHolder vh = new ViewHolder((TextView)

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

关于java - 无法将LinearLayout强制转换为TextView:创建列表和卡片的教程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41453996/

10-12 03:44