簡體   English   中英

無法將LinearLayout強制轉換為TextView:創建列表和卡片的教程

[英]LinearLayout cannot be cast to TextView : tutorial Creating Lists and Cards

我正在嘗試遵循Android官方文檔中的“ 創建列表和卡片”。

我在這里發現有必要在ViewView的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);

並讓ViewHolder構造函數接受一個視圖:

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

其中some_text_view_id是上述布局中的TextView,將由findViewById()獲取

在您的適配器中,您不需要將TextView強制轉換為ViewHolder,並且您還以冗余方式調用ViewHolder。 嘗試這個:

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

不需要這種強制轉換的原因是因為您已經在ViewHolder構造函數下強制轉換了mView ,或者更確切地說,您已經在定義它應該期望的視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM