im trying to populate a list of note/message with check box in the item view.
Ive tried set listener unto the checkbox. sadly upon check box click nothing happens.
if i set listener unto the parent view. its able to trigger onClick method. but this will be triggered each time user click the whole item in the list.
my objectives is more on setting listener for checkbox.
so ill know that user has selected note(s) from the list.
heres my code adapter class together with view holder inner class
public class BroadcastRVA extends RecyclerView.Adapter<BroadcastRVA.BroadcastVH>{
private Context mContext;
private ObservableArrayList<MNote> notes;
private LayoutInflater inflater;
public BroadcastRVA(Context mContext, ObservableArrayList<MNote> notes, LayoutInflater inflater) {
this.mContext = mContext;
this.notes = notes;
this.inflater = LayoutInflater.from(mContext);
}
@Override
public BroadcastRVA.BroadcastVH onCreateViewHolder(ViewGroup parent, int viewType) {
NoteListitemBinding binding = NoteListitemBinding.inflate(inflater);
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.note_listitem, null);
BroadcastVH viewHolder = new BroadcastVH(binding, view);
// create a new view
return viewHolder;
}
@Override
public void onBindViewHolder(BroadcastVH holder, final int position) {
final MNote note = notes.get(position);
holder.cbox.setChecked(note.isSelected());
holder.cbox.setTag(note);
holder.vBinding.setNote(note);
holder.cbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
MNote note = (MNote) cb.getTag();
note.setIsSelected(cb.isChecked());
notes.get(position).setIsSelected(cb.isChecked());
Toast.makeText(
v.getContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
}
});
}
public ObservableArrayList<MNote> getNotes() {
return notes;
}
/**
* Returns the total number of items in the data set hold by the adapter.
*
* @return The total number of items in this adapter.
*/
@Override
public int getItemCount() {
if (notes != null)
return notes.size();
else return 0;
}
public class BroadcastVH extends RecyclerView.ViewHolder {
NoteListitemBinding vBinding;
TextView uuid;
CheckBox cbox;
public BroadcastVH(NoteListitemBinding binding, View view) {
super(binding.getRoot());
this.vBinding = binding;
this.uuid = (TextView) view.findViewById(R.id._UUID);
this.cbox = (CheckBox) view.findViewById(R.id.deleteNote);
}
}
}
Aucun commentaire:
Enregistrer un commentaire