RecyclerView adapter repeats values ​​in the wrong places

I am showing 17 CardViews.

I use RecyclerView to achieve the same.

Each CardView shows general data (format).

Depending on the data received from the JSON file, some additional lines may appear on the map. enter image description here. The following figure shows 2 CardViews (in focus) displaying additional data, the rest displaying general data.

It is displayed correctly. But then, when I scroll down to the View view, the last CardView does not have to display any row, but it copies one of one of the two that we saw in the link above and displays. [Can only send 2 links at a time]

, : enter image description here

?

  • , else

. . , , . :

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

}

:

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();
    }

:

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

.

JSON, , / (), , , /. :

mAdapter.setHasStableIds(true);

, .

. , .

+4
1

, 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" />

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

Source: https://habr.com/ru/post/1621934/


All Articles