简体   繁体   中英

How to get Selected items {list of previously selected array items} set as checked in android recyclerview {Total items}

I'm using android recyclerview for showing list of items, able to select multiple items through MultipleSelect Interface and I need to show the previously selected/checked items are set as checked in edit mode of recyclerview.

I am unable to get this business logic in my code. Can anyone please help me out and I'm new to android development.

I'm getting previoulsy selected items from server in the format arrayList:

InterestsList : Ex:["Antiques-Collecting","Arts-Crafts","Boating"]

This InterestsList items need to set as checked/selected in Recyclerview and again user can able to modify the items need to select more or remove from previoulsy selected and save the Data.

how can I write simple logic for this?

strings ArrayList having the Recycler items list data, InterestsList is the checked items list data.

Adapter Calss :

@Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tvInterestName.setText(interests.get(position).getInterest().toString());

        interestList = model.getInterests();  // PREVIOUSLY SELECTED ITEMS

        //in some cases, it will prevent unwanted situations
        holder.checkBox.setOnCheckedChangeListener(null);

        //if true, your checkbox will be selected, else unselected
        holder.checkBox.setChecked(interests.get(position).isSelected());

        holder.cell.setOnClickListener(v ->
        {
            holder.checkBox.performClick();
        });

        if (interests.get(position).isSelected()) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }

        holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) ->
        {
            guestListener.OnMultipleGuestListener(position, isChecked);
        });

    }

Selecting Multiple items below Interface listener used in my activity class:

 @Override
    public void OnMultipleGuestListener(int position, boolean isChecked) {
        interestFinalList.get(position).setSelected(isChecked);
        interestsAdapter.notifyDataSetChanged();
    }

ScreenShot for required changes in Below Format:

Ex:["Antiques-Collecting","Arts-Crafts","Boating"] Items need to set as checked/selected when i come to this screen.

在此处输入图片说明

Get a single selected result from your model and checked that is equal to the holder text name or not. If both are equals then make the checkbox true. For example:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.tvInterestName.setText(interests.get(position).getInterest().toString());

    //in some cases, it will prevent unwanted situations
    holder.checkBox.setOnCheckedChangeListener(null);

    //if true, your checkbox will be selected, else unselected
   for(int i=0;i<model.getInterests().size();i++)
   if(model.getInterests().get(i).equals(interests.get(position).getInterest())){
        holder.checkBox.setChecked(true);

    }
}

在您的列表中,当复选框未选中时,请维护一个布尔变量,然后相应地更改布尔变量的值(如true和false),然后将其通知您的列表。

and inside your onbind view holder check boolean variable true or false if true then show selected otherwise unselected.

Modify your code like this. hope it will help you.

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.tvInterestName.setText(interests.get(position).getInterest().toString());

    //in some cases, it will prevent unwanted situations
    holder.checkBox.setOnCheckedChangeListener(null);

    //if true, your checkbox will be selected, else unselected
    holder.checkBox.setChecked(interests.get(position).isSelected());

    holder.cell.setOnClickListener(v ->
    {
        holder.checkBox.performClick();
    });

    holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) ->
    {
        interests.get(position).setSelected(isChecked);
        guestListener.OnMultipleGuestListener(position, isChecked);
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM