【问题标题】:Recyclerview Adapter onCreateViewHolder method LinearLayout cannot be cast to TextViewRecyclerview 适配器 onCreateViewHolder 方法 LinearLayout 无法强制转换为 TextView
【发布时间】:2020-08-03 23:09:22
【问题描述】:

所以我只是想设置一个简单的 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”

我无法理解发生了什么,因为我只是按照在线教程进行操作。 如果有人可以帮助我,将不胜感激。

【问题讨论】:

    标签: java android android-layout android-recyclerview


    【解决方案1】:

    当您执行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 从您的布局中移除。如果您的布局中有多个视图,这将是合适的。

    【讨论】:

      【解决方案2】:

      问题出在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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 2021-03-14
        • 2018-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多