I am trying to implement an android listview with checkbox. However, I am unable to get the click event for my checkbox.
I can make the code work if I put the checkbox listener on the adapter class. However I need the information at the main class instead.
Using the main class, I can get the event for listview when clicked but not the checkbox using lv.onItemClick. However, I need to click on the listview once and the checkbox again in order for the checkbox event to be triggered. I need the checkbox event and the listview click event seperately. Any help? Thanks
XML FOR LIST There is still a linear layout outside the Relative layout.
<RelativeLayout
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:descendantFocusability="blocksDescendants"
android:layout_weight="1">
<TextView android:id="@+id/list_item_entry_title_paper"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toLeftOf="@+id/cbPaper"
android:gravity="left"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:fadingEdge="horizontal" />
<TextView android:id="@+id/list_item_entry_summary_paper"
android:layout_alignParentLeft="true"
android:gravity="left"
android:layout_toLeftOf="@+id/cbPaper"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/list_item_entry_title_paper"
android:layout_alignLeft="@id/list_item_entry_title_paper"
android:textAppearance="?android:attr/textAppearanceSmall"
android:singleLine="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
/>
<CheckBox
android:button="@null"
android:id="@+id/cbPaper"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="@drawable/customcbpaper"
android:layout_centerVertical="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:choiceMode="multipleChoice"
android:gravity="right" />
</RelativeLayout>
EVENT HANDLER ON MAIN
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
checkbox = (CheckBox) view.findViewById(R.id.cbPaper);
checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.w("do something", "do something" );
} });
}
});
}
public class CustAdapHeaderWithCbPaper extends ArrayAdapter {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public CustAdapHeaderWithCbPaper(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
protected TextView a;
protected TextView b;
protected CheckBox cb;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder = null;
final int newPosition = position;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}
else //Not a section
{
EntryItemCheckBox ei = (EntryItemCheckBox)i;
v = vi.inflate(R.layout.list_item_entry_cb_paper, null);
holder = new ViewHolder();
holder.a = (TextView)v.findViewById(R.id.list_item_entry_title_paper);
holder.b = (TextView)v.findViewById(R.id.list_item_entry_summary_paper);
holder.cb = (CheckBox)v.findViewById(R.id.cbPaper);
final String titleCheck = ei.title.toString();
holder.cb.setTag(position);
holder.a.setText(ei.title);
holder.cb.setChecked(ei.selected);
v.setTag(holder);
v.setTag(R.id.list_item_entry_title_paper, holder.a);
v.setTag(R.id.list_item_entry_summary_paper, holder.b);
v.setTag(R.id.cbPaper, holder.cb);
}
}
return v;
}
}
checkbox listenerin adapter class only. Then notify themain activity through a custominterface listeners.