RecyclerView 中 onBindViewHolder 的问题
Problem with onBindViewHolder in RecycleView
我无法将我的 textView 放在内部 class 项目的 onBindViewHolder 方法中,我已经完成了 public 和最终,但仍然无法解决它,我尝试了很多示例,这是同样的问题。
package com.example.recyclerview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
String [] items;
public Adapter(Context context, String[] items){
this.context = context;
this.items = items;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.custom_row, viewGroup, false);
Item item = new Item(row);
return item;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
String mCurrent = items[i];
viewHolder.textView.setText(mCurrent); // error here textView cannot resolve
}
@Override
public int getItemCount() {
return items.length;
}
public class Item extends RecyclerView.ViewHolder {
public final TextView textView;
public Item(@NonNull View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item);
}
}
}
您应该更正适配器的通用类型:使用 Adapter.Item
而不是 RecyclerView.ViewHolder
public class Adapter extends RecyclerView.Adapter<Adapter.Item>
然后更新 onCreateViewHolder
和 onBindViewHolder
方法的签名
我无法将我的 textView 放在内部 class 项目的 onBindViewHolder 方法中,我已经完成了 public 和最终,但仍然无法解决它,我尝试了很多示例,这是同样的问题。
package com.example.recyclerview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
String [] items;
public Adapter(Context context, String[] items){
this.context = context;
this.items = items;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.custom_row, viewGroup, false);
Item item = new Item(row);
return item;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
String mCurrent = items[i];
viewHolder.textView.setText(mCurrent); // error here textView cannot resolve
}
@Override
public int getItemCount() {
return items.length;
}
public class Item extends RecyclerView.ViewHolder {
public final TextView textView;
public Item(@NonNull View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item);
}
}
}
您应该更正适配器的通用类型:使用 Adapter.Item
而不是 RecyclerView.ViewHolder
public class Adapter extends RecyclerView.Adapter<Adapter.Item>
然后更新 onCreateViewHolder
和 onBindViewHolder
方法的签名