3

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.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Iganov
  • 85
  • 2
  • 14

2 Answers2

8

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

Community
  • 1
  • 1
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    how to avoid it? can i only update my item appearance instead of data too? – Iganov Sep 21 '18 at 04:09
  • @Iganov its depend on your requirement when you want to add new data inside your **`adapterChart`** – AskNilesh Sep 21 '18 at 04:11
  • i actually dont want to add new data. I just want changed the color of the text – Iganov Sep 21 '18 at 04:14
  • @Iganov than why you are using `adapterChart.addData(mItemsChart);` – AskNilesh Sep 21 '18 at 04:16
  • @Iganov do onething use this`adapterChart.notifyDataSetChanged()` inside your `holder.itemView.setOnClickListener()` – AskNilesh Sep 21 '18 at 04:17
  • i add data cause i need to , but it only need to add data once. I've tried that too but it changes nothing. Thank you for your answer – Iganov Sep 21 '18 at 04:25
  • @Iganov did you try with `adapterChart.notifyDataSetChanged()` after `mItemsChart.clear();` – AskNilesh Sep 21 '18 at 04:26
  • @Iganov share your whole adapter code and also `adapterChart` code with question – AskNilesh Sep 21 '18 at 04:31
  • 1
    i finally understand what happening on my method. it doesnt add new data but it keeps adding this line holder.viewItem.addItemDecoration(horiSpacingDecoration); which changes my layout appearance – Iganov Sep 21 '18 at 04:32
  • 1
    i was assume that this keeps adding new data. it turns out that it wasnt adding new data but adding the margin of each item in my second recyclerview – Iganov Sep 21 '18 at 04:33
  • @Iganov that's greate your issue is solved happy to hear that – AskNilesh Sep 21 '18 at 04:34
  • 1
    @NileshRathod thank you for your answers too. i wouldnt be able to find errors on my code without your answers – Iganov Sep 21 '18 at 04:37
  • @Iganov happy to help you – AskNilesh Sep 21 '18 at 04:38
0

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; 
   }
Faiz Mir
  • 599
  • 5
  • 16