I'm displaying a checked recyclerview in my edit details screen of application. When I go to edit screen I'm able to get the previously selected items are marked as checked working fine, but my challenge is I'm not able to edit the previously selected items along with newly selected items. Only new selected items are updated in my server.
For ex: previuosly I selected {1,2} position items, when I go to edit screen 1,2 items are marked as checked, remaining as marked unchecked. Now if I select new items{3,4} only {3,4} added previously selected items{1,2} not added and sometimes they are not editable even.
Here is my viewholder class:
//in some cases, it will prevent unwanted situations
holder.checkBox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.checkBox.setChecked(allMarketingOptions.get(position).isSelected());
for(int i=0;i<model.getTargetedMarketingOption().size();i++){
if(model.getTargetedMarketingOption().get(i).equals(allMarketingOptions.get(position).getEmailGroupName())){
holder.checkBox.setChecked(true);
}
}
holder.cell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = !holder.checkBox.isChecked();
holder.checkBox.setChecked(isChecked);
guestListener.OnMultipleMarketOptListener(position, isChecked);
}
});
Here is selected items list get in my activity:
@Override
public void OnMultipleMarketOptListener(int position, boolean isChecked) {
allMarketingOptions.get(position).setSelected(isChecked);
allMarketOptionsAdapter.notifyDataSetChanged();
}
You should understand the sliding principle of recyclerView again, and then try to understand what I do below.
in you adapter:
hashMap<Int,Boolean> checkStatusMap,// int is position, Boolean is CheckStatus
in your listener class:
//Ensure that the current location is correct in recyclerView
int mPosition ;
void updateViewByPosition(int position){
mPosition=position
checked= getCheckStatusByPosition(mPosition,checkStatusMap);
holder.checkBox.setChecked(checked);//new
}
@Override
public void onClick(View v) {
boolean newChecked= !getCheckStatusByPosition(mPosition,checkStatusMap);
SaveCheckStatusByPosition(newChecked,mPosition,checkStatusMap);//You need a map or list to store state
holder.checkBox.setChecked(newChecked);//new
}
in your onCreateViewHolder 方法中:
viewHolder.setOnClickListener(listener);//You need to implement a listener
Here is selected items list get in your onBindViewHolder:
@Override
public void onBindViewHolder(int position) {
viewholder.getlistener.updateViewByPosition(position);
}
Where you upload the network:
updateDataByCheckMap(apater.getMap());