简体   繁体   中英

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. 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. Please help.

You overriding it with viewType which is always 0 because there's only one type.

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM