I am currently using checkboxes but I have tried radio buttons as well. I cant seem to get only one item to be selected at a time. My desired outcome is one item to be checked at a time and for that item to remain checked even if the app is closed. here is where I am implementing the checkbox
options1 = new FirebaseRecyclerOptions.Builder<contact>().setQuery(databaseReference1,contact.class).build();
adapter1 = new FirebaseRecyclerAdapter<contact, contactAdapter>(options1) {
@Override
protected void onBindViewHolder(@NonNull final contactAdapter holder, final int position, @NonNull contact model) {
holder.contactName.setText(model.getContactName());
holder.contactPhone.setText(model.getContactPhone());
holder.chk.setTag(holder.getAdapterPosition());
holder.chk.setChecked(position == lastCheckedPos);
holder.chk.setOnCheckedChangeListener(null);
if(holder.getAdapterPosition() == lastCheckedPos){
holder.chk.setChecked(true);
}else{
holder.chk.setChecked(false);
}
holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
holder.chk.setChecked(true);
}else {
holder.chk.setChecked(false);
}
}
});
holder.chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i =0; i <adapter1.getItemCount(); i++){
if(adapter1.getItem(i).isSelected(true)){
number = ((TextView) contactsView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.contactPhone)).getText().toString();
Toast.makeText(getActivity(),"Data Inserted....." + number, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity(),"Error", Toast.LENGTH_LONG).show();
}
}
}
});
}
I am using a firebase recyclerAdapter to pull the data down from firebase. This is my model class
public class contact {
String contactName;
String contactPhone;
private boolean isSelected;
public contact() {
}
public contact(String contactName, String contactPhone) {
this.contactName = contactName;
this.contactPhone = contactPhone;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getContactPhone() {
return contactPhone;
}
public void setContactPhone(String contactPhone) {
this.contactPhone = contactPhone;
}
public boolean isSelected(boolean selected){
return isSelected= selected;
}
}
And this is my adapter class:
public class contactAdapter extends RecyclerView.ViewHolder{
public TextView contactName, contactPhone;
CheckBox chk;
AdapterView.OnItemClickListener itemClickListener;
public contactAdapter(@NonNull View itemView) {
super(itemView);
contactName = itemView.findViewById(R.id.contactName);
contactPhone = itemView.findViewById(R.id.contactPhone);
chk = itemView.findViewById(R.id.myCheckBox);
}
}
I've tried a number of things but a lot of the examples that I've seen dont seem to help. Any suggestions.
Aucun commentaire:
Enregistrer un commentaire