1

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;
}

}

3
  • You should put the checkbox listener in adapter class only. Then notify the main activity through a custom interface listeners. Commented Dec 24, 2013 at 5:20
  • check this out 'stackoverflow.com/questions/20699062/…'. It has the Array adapter in answer section. Commented Dec 24, 2013 at 5:22
  • How can i write the custom interface listeners? i need the results of checkbox in the main class Commented Dec 24, 2013 at 6:45

4 Answers 4

2

You are mentioning android:clickable="false" for the checkBox in the xml...So the checkBox cannot listen the click event..and better put the listener in the Adapter class..

You setOnCheckedChangeListener rather than setOnClickListener

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

i am mentioning android:focusable="false", so that checkbox only get focus explicitly, and by default list view row should get focus.
Hi, it will get the same error even if I use the on click listener
I need the results of listener is main class. what should i do? thanks
1

You can use the property

android:descendantFocusability="afterDescendants" 

in your listview xml. It will be helpful I think. Let me know the result.

Comments

0

outside the first if(i!=null) condition find the checkbox and do the same work you need

for instance:

holder.cb.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) 
                 {
                     Log.w("do something", "do something" );
                 } });

To get the entire RelativeLayout click event give a id in xml and do the same

2 Comments

I need the setClickListener in the main class rather than in the adapter class. Any help? Thanks
@user313408 it's better to write the click listener inside the getView() why do you need that in main. You want the checkbox object to live outside the local scope????
0

First of all remove this line from your Xml file

android:button=null

and add this line to Your checkbox

android:focusable="false"

Then, use this code it will work for you

   checkbox.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                                  if(checkBox.isChecked())
                                     {
                            // do what You want to perform on when check box is checked
                                             }
                                 else
                                {
                                             }
                            }
                         });

and provide separate event for list view like this:

lv.setOnItemClickListener(new OnItemClickListener() 
    {


            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {


            }
    });

2 Comments

Hi, However if i use the onClick(View v) there will be an error
This is because i need the view and i need position of the listview as well. Any help? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.