Fra Red
Fra Red

Reputation: 430

Previous Data is Getting Updated After Scrolling in Recyclerview

I am working on Recyclerview Adapter, My Recyclerview item has 1 textView and 1 Button,

If I click on Button, the TextView Value is Hi it should change to Hello on Recyclerview item. On Button Click The TextView Value is changing properly..(i.e Changing from Hi to Hello)

Problem is when I scroll the recyclerview, after scrolling it shows Previous value again(i.e showing Hi again).

I know there is lot of similar questions being asked on stack overflow & I've tried most of them but nothing seems to work in my case.

My Problem is Solved, Added Working Code.(Added Below Line)

listNet.set(position, new NetData(VALUE));

notifyItemChanged(position);

public class NetAdapter extends RecyclerView.Adapter<NetAdapter.MyViewHolder> {

    Context context;
    List<NetData> listNet;

    public NetAdapter(Context context, ArrayList<NetData> listNet){
        this.context = context;
        this.listNet = listNet;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position){

        final NetData netData=listNet.get(position);

        holder.tv.setText(netData.dnetqty);
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                String VALUE = "Hello"
                holder.tv.setText(VALUE); //Changing Text from Hi to Hello
                //Edited Answer
                listNet.set(position, new NetData(VALUE));
                notifyItemChanged(position);              
            }
        });
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        Button btn;
        TextView tv;
        public MyViewHolder(View itemView){

            super(itemView);
            btn=itemView.findViewById(R.id.btn);
            tv=itemView.findViewById(R.id.tv);
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_recyclerview, parent, false);

        return new MyViewHolder(view);
    }

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

}

I want that, After Changing the TextView Value, the value should be constant it should not change after the Scrolling..

Thank You..

Upvotes: 0

Views: 801

Answers (2)

Vrushi Patel
Vrushi Patel

Reputation: 2431

You have to save changes to your ArrayList< NetData > listNet 's position of changes shown below code sorry don't get your code so just did commented here.

holder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            holder.tv.setText("Hello"); //Changing Text from Hi to Hello

            //here save Hello to your *listNet.get(position)*'s dnetqty property
        }
    });

Upvotes: 1

karan
karan

Reputation: 8843

Setting text to your textview is not enough as recyclerview will recycle views.

You need to save changed text in your dataset and notify those changes to the view. Only then your changes will persist.

Upvotes: 2

Related Questions