[英]RecyclerView Adapter with TextView
My RecyclerView
has 3 columns in a single row. 我的
RecyclerView
单行有3列。 2 are populated from a SQLite
database query and the other with a TextView
. 2是从
SQLite
数据库查询中填充的,另一个是由TextView
填充的。 I'm having troubles with how to have the TextView
populate. 我在如何填充
TextView
遇到麻烦。 The goal is to have the TextView
be set via options of Strings chosen from a set of switch statements. 目的是通过从一组switch语句中选择的Strings选项来设置
TextView
。 However, findViewById
cannot be resolved. 但是,无法解析
findViewById
。 Is there a way to have a TextView
inside onBindViewHolder
? 有没有办法在
onBindViewHolder
有一个TextView
?
RecyclerViewAdapter.java RecyclerViewAdapter.java
public class UpcomingRecyclerViewAdapter extends RecyclerView.Adapter<UpcomingRecyclerViewAdapter.ViewHolder> {
private Cursor mCursor;
public UpcomingRecyclerViewAdapter(Cursor cursor) {
this.mCursor = cursor;
}
@Override
public UpcomingRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_items, parent, false);
return new UpcomingRecyclerViewAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(UpcomingRecyclerViewAdapter.ViewHolder holder, int position) {
if ((mCursor != null) && (mCursor.getCount() != 0)) {
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
String name = mCursor.getString(mCursor.getColumnIndex(VehiclesContract.Columns.VEHICLE_NAME));
String miles = mCursor.getString(mCursor.getColumnIndex(VehiclesContract.Columns.VEHICLE_CURRENT));
String maintenance = (TextView) view.findViewById(R.id.up_list_maintenance);
holder.name.setText(name);
holder.miles.setText(miles);
holder.maintenance.setText(maintenance);
}
}
@Override
public int getItemCount() {
return mCursor != null ? mCursor.getCount() : 0;
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView miles;
TextView maintenance;
public ViewHolder(View itemView) {
super(itemView);
this.name = itemView.findViewById(R.id.upcoming_name);
this.miles = itemView.findViewById(R.id.upcoming_miles);
this.maintenance = itemView.findViewById(R.id.upcoming_maintenance);
}
}
well, in this line 好吧,在这条线
String maintenance = (TextView) view.findViewById(R.id.up_list_maintenance);
you are trying to assign a view to a string , that is sheerly wrong. 您正在尝试将 视图 分配给 string ,这是完全错误的。
You can either assign the value of a textfield which is a string to a string variable
, 您可以
assign the value of a textfield which is a string to a string variable
,
or assign a textfield to a variable of textfield type.
或
assign a textfield to a variable of textfield type.
Fix this first and, then see if it solves your problem. 首先解决此问题,然后查看它是否可以解决您的问题。
String maintenance = (TextView) view.findViewById(R.id.up_list_maintenance);
findViewById returns a View. findViewById返回一个View。 You are trying to cast it a string.
您正在尝试将其转换为字符串。 Try this:
尝试这个:
String maintenance = ((TextView) view.findViewById(R.id.up_list_maintenance)).getText().toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.