jetty
jetty

Reputation: 859

Recycler view List Adapter updates UI incorrectly

I have created a recycler view adapter. Based on the the item in the adapter I am trying to change the UI for that row. When the screen is first loaded the UI is rendered correctly . But once I scroll through, the screen gets updated even for the rows where it should not hav. Here is the code :

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    final OrderItem order = mItems.get(i);
    viewHolder.tvName.setText(getCustomers().get(order.getmConsumerId()).getProfile().getFirstName().toUpperCase() );
    viewHolder.tvDelType.setText(getDeliveryType(order.getmDeliveryType()));
    viewHolder.tvTime.setText(formatTime(order.getmETATime()));//format time before present
    viewHolder.tvAmt.setText("Rs " + order.getmAmount());
    viewHolder.rlvName.setTitleText(order.getmConsumerId().substring(0, 1));
    viewHolder.rlvName.setTitleSize(82f);
    viewHolder.tvDetails.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, OrderDetailsActivity.class);
            Gson gson = new Gson();
            String ord = gson.toJson(order);
            String user = gson.toJson(getCustomers().get(order.getmConsumerId()));
            intent.putExtra("orderItem", ord);
            intent.putExtra("user", user);
            context.startActivity(intent);
        }
    });
    setListeners(viewHolder, order, i);
    //setStatus(viewHolder,order,i);
    if(!order.getmOrderStatus().equals(Constants.ENUM_ORDERS_STATUS_PENDING))
    {
        viewHolder.tvOrderStatus.setText((order.getmOrderStatus().toUpperCase()));
        viewHolder.tvOrderStatus.setVisibility(View.VISIBLE);
        viewHolder.llButtons.setVisibility(View.GONE);
    }

    //viewHolder.rlvName.setBackgroundColor(Color.parseColor("FF0000"));
}

So based on the value my order has, I need to change the UI of the row . After scrolling, it changes the values of the row where it should not have.

Please suggest.

Upvotes: 1

Views: 171

Answers (1)

Mahdi Astanei
Mahdi Astanei

Reputation: 1905

Because of recyler views RAM Management,the recyler view try to load cards from cache and because of your recyler view's cache, the items loads incorrectly. for preventing this you must add a else to your if like this:

if(!order.getmOrderStatus().equals(Constants.ENUM_ORDERS_STATUS_PENDING))
{
    viewHolder.tvOrderStatus.setText((order.getmOrderStatus().toUpperCase()));
    viewHolder.tvOrderStatus.setVisibility(View.VISIBLE);
    viewHolder.llButtons.setVisibility(View.GONE);
}
else
{
    //The Default Item View Settings Here...
}

Upvotes: 1

Related Questions