'RecyclerView Adapter is repeating values at wrong places

I am displaying 17 CardViews.

I am using RecyclerView to achieve the same.

Each CardView shows a common data (format).

Depending on data received from a JSON file, a card may display some additional rows. enter image description here. The following image shows 2 CardViews (in focus) displaying the additional data, the rest displaying the common data.

It appears correctly. But then when I scroll down to bottom of the View, the last CardView is not suppose to display any row, but it does copy one from one of the 2 we saw in the above link and displays. [Can post only 2 links a time]

Then I scroll to the top and it copies the rows from both CardViews that were displaying correctly and repeats here for top 2 cards respectively: enter image description here

What I tried?

  • If, else

Many similar issues show a common solution. One of those answers is here. Thing is I don't know what condition to apply and where. This is my code:

@Override
    public void onBindViewHolder(DataObjectHolder holder, final int position) {


        HashMap<Integer, ShoppingCartSummaryObject> dataItem = new HashMap<Integer, ShoppingCartSummaryObject>(); 
        dataItem = mDataset.get(position);

        TextView textViewComment = holder.textViewComment;

        ShoppingCartSummaryObject obj = new ShoppingCartSummaryObject();
        obj = (ShoppingCartSummaryObject) dataItem.get(position);

        holder.textViewPackageType.setText(obj.getPackageType());
        holder.textViewPackageCode.setText(obj.getPackageCode());
        holder.textViewPackageDesc.setText(obj.getPackageDesc());

        params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        try {
            for (int i = 0; i < obj.getArrayListPart().size(); i++) {

                View view2 = LayoutInflater.from(mContext).inflate(R.layout.detailrow, null);
                holder.advPartDesc = (TextView) view2.findViewById(R.id.advPartDesc);
                holder.advPartnum = (TextView) view2.findViewById(R.id.advPartNum);

                cv = holder.cv;
                advisoryLayout = holder.advisoryLayout;
                empty = holder.empty;
                layoutPackageSummary = holder.layoutPackageSummary;

                holder.detailRow = (RelativeLayout) view2.findViewById(R.id.detailRow);

                rallyLayout = new RelativeLayout(mContext);

                rallyLayout.addView(holder.detailRow);

                cnt+=1;
                rallyLayout.setId(cnt);

                holder.layoutpartList.addView(rallyLayout);

                if (i == 0) { //for adding the first row below standard data

                    params.addRule(RelativeLayout.BELOW, empty.getId() );



       holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
                holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                rallyLayout.setLayoutParams(params);

            } else { //for more than one rows, 2nd row appearing below 1st and 3rd below 2nd and so on

                params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW, cnt - 1);

                holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
                holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                rallyLayout.setLayoutParams(params);
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

and this is how I tried implementing the answers from the shared link:

if(obj.getArrayListPart().size()>0) { /*added the if() condition*/
                    if (i == 0) {

                        params.addRule(RelativeLayout.BELOW, empty.getId());

                        holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);


          holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                    rallyLayout.setLayoutParams(params);

                } else {

                    params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.BELOW, cnt - 1);

                    holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
                    holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                    rallyLayout.setLayoutParams(params);
                }
            }
            else
            {
                holder.advPartnum.setText("abc");
                holder.advPartDesc.setText("abc");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

I also added:

@Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

Nothing worked.

I checked the JSON data, removed one array from the list, and irrespective of the card I remove, it also affects removal of the repeating row values from top/bottom rows (cardviews), depending on the card lying between card with extra details and card on top/bottom. Also tried:

mAdapter.setHasStableIds(true);

but that simply skipped all the additional row data that appears below the common standard.

Kindly help. I have provided whatever I tried.



Solution 1:[1]

Here is the source code which you asked for xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:stackFromEnd="true" />

Activity

 import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import in.ashish29agre.calendar.R;

public class RecyclerViewActivity extends AppCompatActivity {

    private List<Message> messages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        MessagesAdapter adapter = initDefaultsAndGetADapter();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    private MessagesAdapter initDefaultsAndGetADapter() {

        GradientDrawable fromDrawable = new GradientDrawable();
        fromDrawable.setColor(Color.parseColor("#EC407A"));
        fromDrawable.setCornerRadius(16f);

        GradientDrawable toDrawable =  new GradientDrawable();
        toDrawable.setCornerRadius(16f);
        toDrawable.setColor(Color.parseColor("#64B5F6"));
//        toDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
//        toDrawable.getPaint().setStrokeWidth(1);


        messages = new ArrayList<>();
        Random random = new Random();
        Message message;
        for (int i = 0; i < 20; i++) {
            message = new Message();
            int value = random.nextInt(2);
            if (value == Message.TYPE_FROM) {
                message.setMsgType(value);
                message.setContent("Other user says #" + i);
                message.setDrawable(fromDrawable);
            } else if (value == Message.TYPE_TO) {
                message.setMsgType(value);
                message.setContent("You are saying #" + i);
                message.setDrawable(toDrawable);
            } else {
                Log.e("TAG", "Oops");
            }
            messages.add(message);
        }
        return new MessagesAdapter(messages);
    }


    private class MessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<Message> messages;

        public MessagesAdapter(List<Message> messages) {
            this.messages = messages;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if (viewType == Message.TYPE_FROM) {
                View fromLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.from_layout_item, parent, false);
                return new OtherUserMessageHolder(fromLayout);
            } else if (viewType == Message.TYPE_TO) {
                View toLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_layout_item, parent, false);
                return new CurrentUserMessageHolder(toLayout);
            } else {
                throw new IllegalArgumentException("Invalid View type");
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder instanceof OtherUserMessageHolder) {
                configureOtherUserMessageHolder(holder, position);
            } else {
                configureCurrentUserMessageHolder(holder, position);
            }
        }


        private void configureCurrentUserMessageHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder != null && holder instanceof CurrentUserMessageHolder) {
                CurrentUserMessageHolder currentUserMessageHolder = (CurrentUserMessageHolder) holder;
                currentUserMessageHolder.getToContentTv().setText(messages.get(position).getContent());
                currentUserMessageHolder.getToContentTv().setBackground(messages.get(position).getDrawable());
            }
        }

        private void configureOtherUserMessageHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder != null && holder instanceof OtherUserMessageHolder) {
                OtherUserMessageHolder otherUserMessageHolder = (OtherUserMessageHolder) holder;
                otherUserMessageHolder.getFromContentTv().setText(messages.get(position).getContent());
                otherUserMessageHolder.getFromContentTv().setBackground(messages.get(position).getDrawable());
            }
        }

        @Override
        public int getItemCount() {
            return messages.size();
        }

        @Override
        public int getItemViewType(int position) {
            return messages.get(position).getMsgType();
        }

        private final class CurrentUserMessageHolder extends RecyclerView.ViewHolder {

            private TextView toContentTv;

            public CurrentUserMessageHolder(View itemView) {
                super(itemView);
                toContentTv = (TextView) itemView.findViewById(R.id.to_item_tv);
            }

            public TextView getToContentTv() {
                return toContentTv;
            }

        }

        private final class OtherUserMessageHolder extends RecyclerView.ViewHolder {

            private TextView fromContentTv;

            public OtherUserMessageHolder(View itemView) {
                super(itemView);
                fromContentTv = (TextView) itemView.findViewById(R.id.from_item_tv);
            }

            public TextView getFromContentTv() {
                return fromContentTv;
            }

        }
    }

    private class Message {
        private static final int TYPE_FROM = 0;
        private static final int TYPE_TO = 1;

        private int msgType;
        private CharSequence content;
        private Drawable drawable;

        public int getMsgType() {
            return msgType;
        }

        public void setMsgType(int msgType) {
            this.msgType = msgType;
        }

        public CharSequence getContent() {
            return content;
        }

        public void setContent(CharSequence content) {
            this.content = content;
        }

        public Drawable getDrawable() {
            return drawable;
        }

        public void setDrawable(Drawable drawable) {
            this.drawable = drawable;
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 silentsudo