Reputation: 237
I'm trying to implement a checkbox inside a list view. The listView has been generated from a JSON. In my Adapter when I call onCheckedChanged
I want to get the id of that checkbox and then return it back to the server. what if I want to set an id to a checkbox with my own constructor?
public class CheckListAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener{
List List =new ArrayList();
SparseBooleanArray mCheckStates;
boolean[] itemChecked;
public CheckListAdapter(Context context, int resource) {
super(context, resource);
itemChecked = new boolean[10];
mCheckStates = new SparseBooleanArray(10);
}
public void add(@Nullable checkListItems object) {
super.add(object);
List.add(object);
}
@Override
public int getCount() {
return List.size();
}
@Nullable
@Override
public Object getItem(int position) {
return List.get(position);
}
public long getItemId(int position) {
return 0;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row;
row = convertView;
final checkListItemHolder h;
if ( row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_checklist,parent,false);
h = new checkListItemHolder();
h.tx_itemNo = (TextView) row.findViewById(R.id.checkListItemNO);
h.tx_name= (TextView) row.findViewById(R.id.checkListItemName);
h.checkBoxLab= (CheckBox) row.findViewById(R.id.checkBox);
row.setTag(h);
}
else
{
h = (checkListItemHolder)row.getTag();
}
checkListItems L= (checkListItems)this.getItem(position);
h.tx_name.setText(L.getItemName());
Log.w("inja to adaptor","ta inja");
h.tx_itemNo.setText(L.getItemNo());
// h.checkBoxLab.setChecked(false);
// h.checkBoxLab.setChecked(checkedHolder[position]);
h.checkBoxLab.setOnCheckedChangeListener( new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkedHolder[position] = isChecked;
Log.w("yahoo","yahoo");
}
});
return row;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
public boolean[] checkedHolder;
private void createCheckedHolder() {
checkedHolder = new boolean[getCount()];
}
static class checkListItemHolder{
TextView tx_name, tx_itemNo;
CheckBox checkBoxLab;
}
Upvotes: 0
Views: 129
Reputation:
You can simply use buttonView.getId()
inside onCheckedChanged method to get id of the checkbox. This view always refers to the checkbox whose check status has changed as said by Doc.
Upvotes: 0
Reputation: 400
add this line h.checkBoxLab.setTag(position)
under this line h.checkBoxLab= (CheckBox) row.findViewById(R.id.checkBox);
use this to delete the row from your list view :
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Integer index = (Integer) buttonView.getTag();
List.remove(index.intValue());
notifyDataSetChanged();
}
and please explain what exactly do you want to return to server ?
Upvotes: 1