linearlayout不能转换为textview:tutorial创建列表和卡片

nhhxz33t  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(314)

我试着按照android官方文档创建列表和卡片。
我发现有必要将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>
lyfkaqu1

lyfkaqu11#

你把那两个搞混了。假设在

// 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);

并让viewholder构造函数接受视图:

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

其中一些文本视图id是所述布局中的文本视图,将由findviewbyid()获取

qzlgjiam

qzlgjiam2#

在适配器中,您将textview强制转换为viewholder,这是不必要的,而且您还以冗余的方式调用viewholder。试试这个: return new ViewHolder(v) 而不是 ViewHolder vh = new ViewHolder((TextView) 你不需要这个角色的原因是你已经开始了 mView 在viewholder构造函数下,或者更确切地说,您已经定义了它应该期望的视图。

相关问题