[英]RecyclerView does not update a private field in onBindViewHolder after notifydatasetchanged()
I have a RecyclerView in which I have to load different data based on a value which I am changing in an updateList method. 我有一个RecyclerView,我必须根据我在updateList方法中更改的值加载不同的数据。 Following is my code:
以下是我的代码:
public class PerformanceAdapter extends RecyclerView.Adapter<PerformanceAdapter.ViewHolder> {
ArrayList<Performance> list;
Context ctx;
int searchField;
public PerformanceAdapter(ArrayList list, Context ctx) {
this.list = new ArrayList<>();
this.list = list;
this.ctx = ctx;
}
public void updateList(ArrayList<Performance> list, int searchField) {
this.searchField = searchField;
ArrayList tempList = new ArrayList();
tempList.addAll(list);
Log.i("saz","Size on notifyupdate: "+list.size()+" and searchField: "+this.searchField);
this.list.clear();
this.list.addAll(tempList);
notifyDataSetChanged();
}
@Override
public PerformanceAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_performance_cardview, parent, false);
// set the view's size, margins, paddings and layout parameters
searchField = viewType;
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(PerformanceAdapter.ViewHolder holder, int position) {
/* position = (int) convertView.getTag();
Log.i("saz","position value 2nd : "+position);*/
Performance performance = this.list.get(position);
Log.i("saz","searchField: "+searchField);
if (position%2 != 0) {
holder.cv.setCardBackgroundColor(ctx.getResources().getColor(R.color.lighter_gray));
} else {
holder.cv.setCardBackgroundColor(ctx.getResources().getColor(R.color.white));
}
Log.i("saz","searchField: "+searchField);
holder.txtModelName.setText(performance.getModelName());
if (searchField == 0) {
holder.cv.setVisibility(View.GONE);
} else if (searchField == 1) {
Log.i("saz","searchField: "+searchField);
holder.cv.setVisibility(View.VISIBLE);
holder.cv2.setVisibility(View.GONE);
holder.cv3.setVisibility(View.GONE);
holder.cv4.setVisibility(View.GONE);
} else if (searchField == 2) {
holder.cv.setVisibility(View.VISIBLE);
holder.cv2.setVisibility(View.GONE);
holder.cv3.setVisibility(View.GONE);
holder.cv4.setVisibility(View.GONE);
}
}
@Override
public int getItemViewType(int position) {
return searchField;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
Log.i("saz","List size in recycler: "+list.size());
return this.list.size();
}
Then I call 然后我打电话
updateList(list, 0)
updateList(list, 1)
or updateList(list, 2)
on click of a button. 点击按钮。
But I always get the searchField as 0 in my bindViewHolder. 但我总是在我的bindViewHolder中将searchField设为0。 Please help.
请帮忙。
You overriding it with viewType which is always 0 because there's only one type. 你用viewType覆盖它,它始终为0,因为只有一种类型。
@Override public
PerformanceAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_performance_cardview, parent, false); // set the view's size, margins, paddings and layout parameters
searchField = viewType;
ViewHolder vh = new ViewHolder(v);
return vh;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.