Iganov
Iganov

Reputation: 85

What does notifyDataSetChanged() do on recyclerview? why it keeps adding new data everytime i call notifyDataSetChanged()?

I'm trying to implements this method on my recyclerview method Changing background color of selected item in recyclerview

I followed the script correctly and it works the way i want to. Yes it changes the color but everytime i clicked on the item, seems like it keeps adding new data. I'm using 2 recyclerview.

My method on first recyclerview called another method to drawn a recyclerview again. It is like when we called first recyclerview it also calls second recyclerview.

This is my script :

    @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.viewItem.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true);
    holder.viewItem.setLayoutManager(layoutManager);

    HoriSpacingDecoration horiSpacingDecoration = new HoriSpacingDecoration(Utils.convertDpToPixel(10,context));
    holder.viewItem.addItemDecoration(horiSpacingDecoration);

    adapterChart = new ChartItemAdapter(context);
    holder.viewItem.setAdapter(adapterChart);

    mItemsChart.clear();

        holder.textView.setText(mBulan.get(position));
        int dataTertinggi = Integer.parseInt(Collections.max(mItems));
        int botol = dataTertinggi/5;
        int ci = niceround(dataTertinggi);
        Log.d(TAG, "onBindViewHolder: pembulatan " + ci);
            ukuran = Integer.parseInt(mItems.get(position))/botol;

            if(Integer.parseInt(mItems.get(position))%10 < botol)
            {
                ukuran2=1;
                ukuran+=ukuran2;
            }
            else
            {
                ukuran2=1;
                ukuran+=ukuran2;
            }

        for(int y=0;y<ukuran;y++)
        {
            mItemsChart.add(String.valueOf(y));
        }
        adapterChart.addData(mItemsChart);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,mItems.get(position) + " Bottle" ,Toast.LENGTH_SHORT).show();

                prevPos = position;
                mItemsChart.clear();
                notifyDataSetChanged();
            }
        });
    if(prevPos == position)
    {
        holder.textView.setTextColor(context.getResources().getColor(R.color.colorPrimary));
    }
    else
    {
        holder.textView.setTextColor(context.getResources().getColor(R.color.colorAccent));
    }
}

I already found the problem is it when i called notifydatasethaschanged() it also called the adapter to add data. I'm stuck and dont know what to do. Perhaps anyone could find a solution for me.

Upvotes: 3

Views: 4039

Answers (2)

Faiz Mir
Faiz Mir

Reputation: 609

When you call notifyDataSetChanged() what it Does it call the OnBindView and in your on bind view your are adding data in forloop so it add data again

You can do one thing to overcome with this problem

Declare a boolean variable isAdded = false;

 if(!isAdded){
    //your for loop here 
    //add data here
   //and make isAdded true
    isAdded = true; 
   }

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69724

Your first question

What does notifyDataSetChanged() do on recyclerview?

From Docs

notifyDataSetChanged

  1. Notify any registered observers that the data set has changed.

  2. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.

  3. means whenever you called notifyDataSetChanged() it call onBindViewHolder()

Your second question

why it keeps adding new data everytime i call notifyDataSetChanged()?

Because you are using adding new data in your adapterChart using below line adapterChart.addData(mItemsChart); inside Your onBindViewHolder

it means whenever your onBindViewHolder is called you are adding data in your adapterChart

Upvotes: 8

Related Questions