簡體   English   中英

Recyclerview Adapter onCreateViewHolder 方法 LinearLayout 無法轉換為 TextView

[英]Recyclerview Adapter onCreateViewHolder method LinearLayout cannot be cast to TextView

所以我只是想設置一個簡單的 Recyclerview 來顯示數組中的字符串。 這是我設置的適配器:

package com.example.workoutapp1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class WorkoutActivityAdapter extends RecyclerView.Adapter<WorkoutActivityAdapter.MyViewHolder> {
    private String[] 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 MyViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public MyViewHolder (TextView v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.workout_text_view);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public WorkoutActivityAdapter(String[] myDataset) {
        mDataset = myDataset;
    }


    // Create new views (invoked by the layout manager)
    @Override
    public WorkoutActivityAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        TextView contactView = (TextView) inflater.inflate(R.layout.workout_item, parent, false);

        // Return a new holder instance
        WorkoutActivityAdapter.MyViewHolder viewHolder = new WorkoutActivityAdapter.MyViewHolder(contactView);
        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element\
        holder.textView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

workout_item.xml 是一個簡單的線性布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp" >

    <TextView
        android:id="@+id/workout_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />


</LinearLayout>

每次我運行它時,應用程序都會崩潰,因為在我嘗試膨脹自定義布局時出現致命異常:

“java.lang.ClassCastException:android.widget.LinearLayout 無法轉換為 android.widget.TextView”

我無法理解發生了什么,因為我只是在網上學習教程。 如果有人可以幫助我,那將不勝感激。

當您執行inflater.inflate(R.layout.workout_item, parent, false)時,您正在膨脹 XML 文件的完整布局。 該 XML 文件的最頂層元素是LinearLayout ,因此返回的擴展布局是LinearLayout ,而不是TextView

你有兩個選擇:

  1. 完全刪除 LinearLayout。

這意味着您的布局將僅包含您的單個TextView ,這將使您的轉換成功。

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/workout_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    />
  1. 更新您的 ViewHolder 以采用完整布局。

在這里,您將更新WorkoutActivityAdapter.MyViewHolder以采用完整布局並使用view.findViewById(R.id.workout_text_view)從您的布局中獲取 TextView。 如果您的布局中有多個視圖,這將是合適的。

問題出在onCreateViewHolder方法上。

您正在使用

TextView contactView = (TextView) inflater.inflate(R.layout.workout_item, parent, false);

但是workout_item布局的根視圖是LinearLayout ,您不能將LinearLayout轉換為TextView

使用一些東西:

    @Override
    public WorkoutActivityAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View view = inflater.inflate(R.layout.workout_item, parent, false);

        // Return a new holder instance
        WorkoutActivityAdapter.MyViewHolder viewHolder = new WorkoutActivityAdapter.MyViewHolder(view);
        return viewHolder;
    }

和:

public static class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView textView;
    public MyViewHolder (View v) {
        super(v);
        textView = (TextView) v.findViewById(R.id.workout_text_view);
    }
}

暫無
暫無

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

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