Select all checkboxes in RecyclerView - android

How can I select all checkboxes in recyclerView?
I try to do it like this:
in Adapter:
public void selectAll(){
Log.e("onClickSelectAll","yes");
isSelectedAll=true;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
PersonDTO item = dataSet.get(position);
holder.tvName.setText(item.getName());
if (!isSelectedAll) holder.cbSelect.setSelected(false);
else holder.cbSelect.setSelected(true);}
In layout Activity I have a button with onClickListener:
private void onClickSelectAll(View view) {
getSelectPersonsAdapter().selectAll();
}

You made a simple mistake here
if (!isSelectedAll) holder.select_item.setChecked(false);
else holder.select_item.setChecked(true);
change setSelected into setChecked thats it

for both Select All and Unselect All
activty
checkall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkall.isChecked()){
categoryAdapter.selectAll();
}
else {
categoryAdapter.unselectall();
}
}
});
in adapter
public void selectAll(){
isSelectedAll=true;
notifyDataSetChanged();
}
public void unselectall(){
isSelectedAll=false;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(#NonNull final MyviewHolder holder, int position) {
final CategoryListItems list = categoryListItems.get(position);
holder.text.setText(list.getName());
if (!isSelectedAll){
holder.checkbox.setChecked(false);
}
else holder.checkbox.setChecked(true);

Select all Checkbox in recycler view with data binding
In adapter assign globally boolean parameter
public boolean isAllChecked = false;
Create select all method
public void setAllChecked(boolean isAllChecked) {
this.isAllChecked = isAllChecked;
notifyDataSetChanged();
}
In onBind view to write here
if (!isAllChecked) holder.ulbCheckboxBinding.chkBoxUlbName.setChecked(false);
else holder.ulbCheckboxBinding.chkBoxUlbName.setChecked(true);
Go to your activity and used onclick listener
binding.checkSelectAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.setAllChecked(binding.checkSelectAll.isChecked());
adapter.notifyDataSetChanged();
}
});
thank you
#android

Related

Having challenge in RecyclerView item's onClickListener

I have an item list which are placed under a RecyclerView. On any item, there a quantity(add or subtract). When add is selected, the quantity should increase one.
But when i clicked on '+' for the first time, the value of quantity is setting to 1 and soon after resetting to 0, but when i click on second time the value is becoming '1'. Similarly when i click on next time the value is setting to 2 and after a fraction of second it is still getting to 1 and i click on another time the value is becoming 2. Why by first click is not getting recognized? Can someone please help me here?
public class ItemListAdapter extends RecyclerView.Adapter<ItemListAdapter.ItemViewHolder>{
private Context mContext;
private List<Items> itemsList;
private OnItemListListner onClick;
public ItemListAdapter(Context mContext,List<Items> itemsList)
{
this.mContext=mContext;
this.itemsList=itemsList;
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(mContext);
View view=inflater.inflate(R.layout.item_details_layout,null);
return new ItemListAdapter.ItemViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ItemViewHolder holder, final int position) {
Items items=itemsList.get(position);
Log.d("onBindViewHolder",items.getItemName());
holder.itemNameTV.setText(items.getItemName());
String strItemPrice=mContext.getResources().getString(R.string.Rs)+" "+String.valueOf(items.getItemPrice());
holder.itemPriceTV.setText(strItemPrice);
holder.itemDescTV.setText(items.getItemDescription());
Glide.with(mContext).load(items.getItemImagePath()).into(holder.imageView);
holder.itemAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int intCurrentQuantity=Integer.valueOf(holder.itemQuantity.getText().toString());
intCurrentQuantity+=1;
Log.d("myQuantity",String.valueOf(intCurrentQuantity));
holder.itemQuantity.setText(String.valueOf(intCurrentQuantity));
notifyItemChanged(holder.getAdapterPosition());
}
});
holder.itemNameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemListClick(position);
}
});
holder.itemPriceTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemListClick(position);
}
});
holder.itemDescTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemListClick(position);
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemListClick(position);
}
});
}
#Override
public int getItemCount() {
return itemsList.size();
}
public void SetOnClick(OnItemListListner onClick)
{
this.onClick=onClick;
}
class ItemViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView itemNameTV,itemPriceTV,itemDescTV,itemQuantity,itemSubtract,itemAdd;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.itemImageView);
itemNameTV=itemView.findViewById(R.id.itemNameTextView);
itemPriceTV=itemView.findViewById(R.id.itemPriceTextView);
itemDescTV=itemView.findViewById(R.id.itemDescritionTextView);
itemQuantity=itemView.findViewById(R.id.itemQuantity);
itemSubtract=itemView.findViewById(R.id.subtractQuantity);
itemAdd=itemView.findViewById(R.id.addQuantity);
}
}
public interface OnItemListListner{
void onItemListClick(int position);
}
}
you should have the quantity property in Item model class and have to follow the code I added below.
holder.itemAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int quantity = item[getAdapterPosition()].quantity;
quantity = quantity +1;
item[getAdapterPosition()].setQuantity(quantity);
notifyItemChanged(getAdapterPosition());
}
});
Thanks.I am sure this will work for you.
Reviewd your code and then after i'm just agreed with Harsh's Answer
there is a two way to set your adapter. most commonly used Model for storing and retriving data.
You need to create a one model class and and set data first into the model then after get data from model and set into your adapter / notify your adapter.
You need to create one variable in model class like below snippet:
public class Item {
String mQty = "";
public String getmQty() {
return mQty;
}
public void setmQty(String mQty) {
this.mQty = mQty;
}
}
Helpful Answer: Click Me
Recycler view recycles the views on scroll and resets its content, so you have to persist the quantity qty variable. You can do it by two ways
Store quantity inside your Items model.
Persist quantity in seperate HashMap as item.getId() as key and qty as value.
then get and set from the model object or hashmap.
Check updated code
holder.itemAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int intCurrentQuantity=Integer.valueOf(holder.itemQuantity.getText().toString());
++intCurrentQuantity; //use this
Log.d("myQuantity",String.valueOf(intCurrentQuantity));
holder.itemQuantity.setText(String.valueOf(intCurrentQuantity));
notifyItemChanged(holder.getAdapterPosition());
}
});

Disabling a RecyclerView

I'm facing the next problem.
I have a RecyclerView with the Adapter, the items, etc.
What I need is: Select an item in the recycler, change the color of that item, and then I have to disable the rest of the other items. It's like a RadioButton list.
I select one and disable the others. If I select again the same item, enable all the list.
I already have the onClick Button listener. I need to know if I have to reload again de list, If I have to loop and disable item by item, etc.
Thanks
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Human> humans;
private HumanClick selectedHuman;
#Inject
public MyAdapter() {
}
public void init(List<Human> humanList, SelectedHumanClick humanClick){
this.humans = humanList;
this.humanClick = humanClick;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_Human, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, int position) {
final Human human = Humans.get(position);
holder.bind(Humans.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedHumanClick.onSelectedHumanClick(Human);
}
});
}
#Override
public int getItemCount() {
return humans != null ? Humans.size() : 0;
}
public final class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.text_view_human_name)
TextView textView;
#BindView(R.id.image_view_profile)
ImageView imageViewProf;
#BindView(R.id.image_view_radio_btn)
ImageView imageViewRB;
private boolean isChecked = false;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(final Human Human) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
humanClick.onSelectedHumanClick(Human);
}
});
textView.setText(human.getName());
}
public void changeRBImage() {
if(!isChecked){
isChecked = true;
imageViewRB.setBackground(ContextCompat.getDrawable(imageViewRB.getContext(), R.drawable.selected));
}
else{
isChecked = false;
imageViewRB.setBackground(ContextCompat.getDrawable(imageViewRB.getContext(), R.drawable.not_selected));
}
}
}
My Recycler is defined inside a fragment here:
public class HumansFragment implenets HumanClick{
#BindView(R.id.recycler_humans)
RecyclerView humansRecyclerView;
#Inject
MyAdapter myAdapter;
.
.
.
public void loadHumans(List<Human> humans) {
myAdapter.init(Humans, this);
humansRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
humansRecyclerView.setAdapter(myAdapter);
}
#Override
public void humansClick(Human human) {
//TODO
}
}
I'm thinking in something like this, using setOnItemClickListener and check the position of the item clicked. The position is the index of the item clicked check the last event and new, if both are equal change color, if not dont do anything like disabling.
private ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
textView = (TextView)view.findViewById(android.R.id.text1);
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "position = " + getLayoutPosition(), Toast.LENGTH_SHORT).show();
//go through each item if you have few items within recycler view
if(getLayoutPosition()==0){
//Do whatever you want here
}else if(getLayoutPosition()==1){
//Do whatever you want here
}else if(getLayoutPosition()==2){
}else if(getLayoutPosition()==3){
}else if(getLayoutPosition()==4){
}else if(getLayoutPosition()==5){
}
//or you can use For loop if you have long list of items. Use its length or size of the list as
for(int i = 0; i<exampleList.size(); i++){
}
}
}
Or something smaller
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, recyclerView ,new
RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
// do whatever
}
#Override public void onLongItemClick(View view, int position) {
// do whatever
}
})
);

Access arraylist from a recycleradapter class

I want to access an ArrayList from a nonactivity class to a fragment.
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
holder.qty.setText(String.valueOf(++qt));
mDataset.get(position).setQty(String.valueOf(qt));
finalDataset.add(new ItemDetails(ItemFragment.getInstance().cat_id,String.valueOf(holder.itemid.getText()),String.valueOf(holder.itemname.getText()),String.valueOf(holder.rate.getText()),String.valueOf(holder.qty.getText())));
}
});
holder.minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(qt==0)
{
holder.qty.setText(String.valueOf(0));
}
else
{
holder.qty.setText(String.valueOf(--qt));
}
mDataset.get(position).setQty(String.valueOf(qt));
finalDataset.add(new ItemDetails(ItemFragment.getInstance().cat_id,String.valueOf(holder.itemid.getText()),String.valueOf(holder.itemname.getText()),String.valueOf(holder.rate.getText()),String.valueOf(holder.qty.getText())));
}
});
}
finalDataset is the mentioned ArrayList. I added the values to the ArrayList inside the onBindViewHolder method in recycleradapter class. And I want to access it in another fragment.
Create method to return List in adapter class,
public List<ItemDetails> getDataset() {
return finalDataset;
}
In fragment class access it using,
List<ItemDetails> finalDataset = adapter.getDataset();

On Scrolling Check Boxes are getting Unchecked or Vice versa in a RecyclerView

I have a check box in RecyclerView Adapter's View Item.
When scrolling, some check boxes are getting checked and some are getting unchecked.
I have taken help from this and this
But I am unable to find the exact solution to this.
Here is my code
public class AllContactsAdapter extends RecyclerView.Adapter<AllContactsAdapter.ViewHolder> {
private Context context;
private ArrayList<PhoneContactsModel> listOfContacts = new ArrayList<>();
PhoneContactsModel phoneContactsModel;
private ArrayList<PhoneContactsModel> copyOfListOfContacts = new ArrayList<>();
public AllContactsAdapter(Context context, ArrayList<PhoneContactsModel> listOfContacts) {
this.context = context;
this.listOfContacts = listOfContacts;
// copyOfListOfContacts.addAll(listOfContacts);
}
#Override
public AllContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.
adapter_load_allusers_contacts_listitem, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final AllContactsAdapter.ViewHolder holder, final int position) {
Log.d("tag", "adapter========3");
phoneContactsModel = listOfContacts.get(position);
holder.name.setText(phoneContactsModel.getContactName());
holder.phoneNumber.setText(phoneContactsModel.getContactNumber());
holder.checkBox.setSelected(phoneContactsModel.isChecked());
holder.contactLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.checkBox.isChecked()) {
phoneContactsModel.setChecked(false);
holder.checkBox.setChecked(false);
copyOfListOfContacts.remove(listOfContacts.get(position));
} else {
phoneContactsModel.setChecked(true);
holder.checkBox.setChecked(true);
copyOfListOfContacts.add(listOfContacts.get(position));
}
}
});
}
#Override
public int getItemCount() {
return listOfContacts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name, phoneNumber;
private CheckBox checkBox;
private LinearLayout contactLayout;
public ViewHolder(View view) {
super(view);
name = view.findViewById(R.id.contactNameId);
phoneNumber = view.findViewById(R.id.contactNumberId);
checkBox = view.findViewById(R.id.checkBoxId);
contactLayout = view.findViewById(R.id.contactLayoutId);
}
}
}
For example, I have checked 1,2,3 items in list. When I scrolled down and come up , then 13 is checked and 2 is unchecked along with other some items.Please help me.
Any solution is appreciated.
A simple way arround is do not use OnCheckedChangeListener just set OnClickListener on Check box . Move checkBox onclick inside ViewHolder class.
public class ViewHolder1 extends RecyclerView.ViewHolder implements View.OnClickListener {
CheckBox checkBox;
public ViewHolder1(View itemView) {
super(itemView);
checkBox = (TextView) itemView.findViewById(R.id.checkBox);
checkBox.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(getAdapterPosition()!=-1) {
phoneContactsModel.get(getAdapterPosition()).setChecked(!phoneContactsModel.get(getAdapterPosition()).isChecked());
notifyItemChanged(getAdapterPosition());
}
}
}
And
public void onBindViewHolder(final AllContactsAdapter.ViewHolder holder, final int position) {
holder.checkBox.setChecked(phoneContactsModel.isChecked());
}
notify your adapter.
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
phoneContactsModel.setChecked(b);
//notifyItemChanged(position);(if you want to notify change in single item)
//or
//notifyDataSetChanged();(if you want to notify change in all)
}
});
no need to use this line of code twice
holder.checkBox.setSelected(phoneContactsModel.isChecked());
//this section seems buggy i assume you want to add the item to the list when ever the check box is checked and remove it from the list when ever the check box is unchecked.
holder.contactLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (phoneContactsModel.isChecked()) {
holder.checkBox.setSelected(false);
copyOfListOfContacts.remove(listOfContacts.get(position));
} else {
holder.checkBox.setSelected(true);
copyOfListOfContacts.add(listOfContacts.get(position));
}
}
}
);
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final PhoneContactsModel phoneContactsModel = listOfContacts.get(position);
holder.name.setText(phoneContactsModel.getContactName());
holder.phoneNumber.setText(phoneContactsModel.getContactNumber());
holder.checkBox.setChecked(/* gets value from the modal */ phoneContactsModel.isChecked());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean value) {
if (compoundButton.isPressed()) {
holder.checkBox.setChecked(value);
// set the value into the modal.
phoneContactsModel.setChecked(value);
if (value) {
copyOfListOfContacts.remove(phoneContactsModel);
} else {
copyOfListOfContacts.add(phoneContactsModel);
}
}
}
});
}
Change your onBindViewHolder method like this. There won't be any unnecessary checks.

how can refresh list when click button in item listview

i have recyclerview and button on the all item of list. when i click the button in my adapter item deleted and i want refresh recycler and show new data.
#Override
public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
lietner = lietners.get(position);
holder.txt_en.setText(lietner.getText_en());
holder.txt_pe.setText(lietner.getText_pe());
holder.time.setText(lietner.getTime());
holder.data.setText(HelperCalendar.g2j(lietner.getTime_create()));
holder.next_level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
G.databaseOpenHelper.goToNextLevel(lietners.get(position).getId());
}
});
}
For re-initializing the recycler view and get callback on onBindViewHolder
simple call notifyDataSetChanged();
#Override
public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
lietner = lietners.get(position);
holder.next_level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// delete the item, or change the data.
notifyDataSetChanged();
}
});
}
i do this and it work for me!
#Override
public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
lietner = lietners.get(position);
holder.txt_en.setText(lietner.getText_en());
holder.txt_pe.setText(lietner.getText_pe());
holder.time.setText(lietner.getTime());
holder.data.setText(HelperCalendar.g2j(lietner.getTime_create()));
holder.next_level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
G.databaseOpenHelper.goToNextLevel(lietners.get(position).getId());
lietners.remove(position);
notifyDataSetChanged();
}
});
}

Categories

Resources