androidandroid-togglebuttonandroid-diffutilsandroid-listadapter

How to change the data of an item at once using DiffUtil?


enter image description here

When I press the toggle button, I want to change the units of the list of the currently displayed recycler views at once.

I used ListAdapter + DiffUtil to display the recycler view.

The way I tried to implement this feature is to load the current list when the toggle button is pressed.

Then, after resetting the new toggle unit values ​​for the current lists, I used submitList() to update the list.

But this was the wrong way.

My guess is because the variable created for the value of the list loaded to be updated has the same reference value, so the value changed at the same time.

In other words, there is no change because the values ​​of the update list and the existing list are the same.

What can I do to solve this problem?

RoutineDetailModel.java

public class RoutineDetailModel {
    public int id;
    private int set = 1;
    public static String unit = "kg";

    public RoutineDetailModel() {
        Random random = new Random();
        this.id = random.nextInt();
    }

    public RoutineDetailModel(int set) {
        Random random = new Random();
        this.id = random.nextInt();
        this.set = set+1;
    }

    public int getSet() {
        return set;
    }

    public int getId() {
        return id;
    }

    public String getWeight() {
        return weight;
    }

    public String getUnit() {
        return unit;
    }

    @Override
    public int hashCode() {
        return Objects.hash(set, weight); // getWeight를 호출하면 더 다양하게 되나?
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if(obj != null && obj instanceof RoutineDetailModel) {
            RoutineDetailModel model = (RoutineDetailModel) obj;
            if(this.id == model.getId()) {
                return true;
            }
        }
        return false;
    }
}

MainActivity.java

public class WriteRoutineActivity extends AppCompatActivity implements WritingCommentDialogFragment.OnDialogClosedListener {
    List<RoutineModel> items;
    RoutineListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_routine);
        
        listAdapter.setOnRoutineClickListener(new RoutineListAdapter.OnRoutineItemClickListener() {
            @Override
            public void onUnitBtnClicked(int curRoutinePos, String unit) {
                Object obj = listAdapter.getRoutineItem(curRoutinePos);
                RoutineModel item = (RoutineModel) obj;
                if(obj instanceof RoutineModel) {
                    for(RoutineDetailModel detailItem : item.getDetailItemList()) {
                        detailItem.setUnit(unit);
                    }
                    listAdapter.submitList(getUpdatedList());
                }
            }
        });
    }
}

Please tell me if you need more information


Solution

  • To change all the items you need to use notifyDataSetChanged() otherwise you cannot update ALL the items.

    In order to do so, you must create a method inside your adapter which does the following ->

    public void updateItems(String unit) {
        for({YOUR ITEM TYPE} item: {YOUR LIST}) {
           item.unit = unit;
        }
        
        notifyDataSetChanged();
    }
    

    And call this method when you want to change all units.

     yourAdapter.updateItems("Kg");