[英]Recyclerview Adapter onCreateViewHolder method LinearLayout cannot be cast to TextView
So I'm just trying to set up a simple Recyclerview to display strings from an array.所以我只是想设置一个简单的 Recyclerview 来显示数组中的字符串。 Here's the adapter that I have set up:
这是我设置的适配器:
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 is a simple linearlayout as follows: 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>
Every time I run this, the app crashes, as there is a fatal exception where I'm trying to Inflate the custom layout:每次我运行它时,应用程序都会崩溃,因为在我尝试膨胀自定义布局时出现致命异常:
"java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView"
“java.lang.ClassCastException:android.widget.LinearLayout 无法转换为 android.widget.TextView”
I'm having trouble understanding what's going on, as I'm simply following tutorials online.我无法理解发生了什么,因为我只是在网上学习教程。 If anybody could help me out, that would be greatly appreciated.
如果有人可以帮助我,那将不胜感激。
When you do inflater.inflate(R.layout.workout_item, parent, false)
, you are inflating the full layout of your XML file.当您执行
inflater.inflate(R.layout.workout_item, parent, false)
时,您正在膨胀 XML 文件的完整布局。 The topmost element of that XML file is a LinearLayout
, so the returned inflated layout is a LinearLayout
, not a TextView
.该 XML 文件的最顶层元素是
LinearLayout
,因此返回的扩展布局是LinearLayout
,而不是TextView
。
You have two options:你有两个选择:
This would mean your layout would only contain your single TextView
, which would make your casting succeed.这意味着您的布局将仅包含您的单个
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"
/>
Here you'd update your WorkoutActivityAdapter.MyViewHolder
to take the full layout and use view.findViewById(R.id.workout_text_view)
to get the TextView out of your layout.在这里,您将更新
WorkoutActivityAdapter.MyViewHolder
以采用完整布局并使用view.findViewById(R.id.workout_text_view)
从您的布局中获取 TextView。 This would be appropriate if you had more than one view in your layout.如果您的布局中有多个视图,这将是合适的。
The issue is on the onCreateViewHolder
method.问题出在
onCreateViewHolder
方法上。
You are using您正在使用
TextView contactView = (TextView) inflater.inflate(R.layout.workout_item, parent, false);
but the root view of the workout_item
layout is a LinearLayout
and you can not cast a LinearLayout
to a TextView
.但是
workout_item
布局的根视图是LinearLayout
,您不能将LinearLayout
转换为TextView
。
Use something:使用一些东西:
@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;
}
and:和:
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.