53

Possible Duplicate:
Android: Binding data from a database to a CheckBox in a ListView?

i want to use a ListView with the items having following layout

------------------------- 
 [CB]    TV            TV
-------------------------

CB is a checkbox and TV is a Textview.

Now i've read somewhere that you can't have clickable items in a ListView. If you have some then you won't be able to click on the ListItems. But if I look at the GoogleMail app, this is possible. I can mark several messages with the checkbox (and then select an action for them) or i can click on the ListItem (or scroll with the dpad) to get to another screen. Does someone has code/example how this is possible?

Community
  • 1
  • 1
codie4711
  • 798
  • 3
  • 8
  • 7
  • I don't have the code atm but it is possible because move, drag, click & longClick is not the same event. And also the hitbox of the checkbox is on top of the listview. – Fredrik Mar 24 '11 at 09:58
  • Look at the code [here](http://stackoverflow.com/questions/1505751/android-binding-data-from-a-database-to-a-checkbox-in-a-listview) You can select multiple items in listview. Check [here](http://appfulcrum.com/?p=281) With Text and Image example : [here](http://appfulcrum.com/?p=311) – Kartik Domadiya Mar 24 '11 at 09:58
  • 3
    This question is not a duplicate. The other question is about how to make CursorAdapter bind the right db columns to particular views. This question isn't about binding the data at all, it's about how to make a checkbox clickable in a ListView. – Dan Hulme Sep 08 '13 at 10:54
  • This is not a duplicate... This is about ListView + CheckBox,.. and not about data binding! Dumb moderators...!!!! – kravemir Apr 25 '15 at 09:21

5 Answers5

100

Set the CheckBox as focusable="false" in your XML layout. Otherwise it will steal click events from the list view.

Of course, if you do this, you need to manually handle marking the CheckBox as checked/unchecked if the list item is clicked instead of the CheckBox, but you probably want that anyway.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
PacificSky
  • 3,422
  • 2
  • 25
  • 24
  • 30
    Setting focusable="false" for checkbox allows me to click the listview items, but it doesn't stop me from clicking on the checkbox itself. For this, I also need to set clickable="false" for the checkbox. – Anuj Nov 28 '12 at 17:41
  • how to allow stealing of click on checkbox on item click of list view android? – Cyph3rCod3r Mar 07 '14 at 06:06
  • I tried setting focausable = "false" checkbox of recyclerview. It was working fine, i could be able to click the list item. But when I tried to add checkedChangeLstener on checkbox it blocked the list item. Now can't click on list item. @PacificSky – Sid Apr 16 '16 at 10:35
19

Set the listview adapter to "simple_list_item_multiple_choice"

ArrayAdapter<String> adapter;

List<String> values; // put values in this

//Put in listview
adapter = new ArrayAdapter<UserProfile>(
this,
android.R.layout.simple_list_item_multiple_choice, 
values);
setListAdapter(adapter);    
Erik Bergstedt
  • 315
  • 2
  • 6
  • 1
    Those built in layouts are useful only if you want a simple layout. He's showing that he wants to do a checkbox with 2 text views. – Stealth Rabbi Jul 24 '14 at 13:31
9
holder.checkbox.setTag(row_id);

and

holder.checkbox.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckBox c = (CheckBox) v;

                    int row_id = (Integer) v.getTag();

                    checkboxes.put(row_id, c.isChecked());


                }
        });
max4ever
  • 11,909
  • 13
  • 77
  • 115
7

this code works on my proyect and i can select the listview item and checkbox

<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true" >

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:text="" >
    </CheckBox>

</LinearLayout>
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Borja
  • 1,318
  • 13
  • 18
5

Below code will help you:

public class DeckListAdapter extends BaseAdapter{

      private LayoutInflater mInflater;
        ArrayList<String> teams=new ArrayList<String>();
        ArrayList<Integer> teamcolor=new ArrayList<Integer>();


        public DeckListAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            teams.add("Upload");
            teams.add("Download");
            teams.add("Device Browser");
            teams.add("FTP Browser");
            teams.add("Options");

            teamcolor.add(Color.WHITE);
            teamcolor.add(Color.LTGRAY);
            teamcolor.add(Color.WHITE);
            teamcolor.add(Color.LTGRAY);
            teamcolor.add(Color.WHITE);


        }



        public int getCount() {
            return teams.size();
        }


        public Object getItem(int position) {
            return position;
        }


        public long getItemId(int position) {
            return position;
        }

       @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;


            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.decklist, null);

                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.deckarrow);
                holder.text = (TextView) convertView.findViewById(R.id.textname);

             .......here you can use holder.text.setonclicklistner(new View.onclick.

                        for each textview


                System.out.println(holder.text.getText().toString());

                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();
            }



             holder.text.setText(teams.get(position));

             if(position<teamcolor.size())
             holder.text.setBackgroundColor(teamcolor.get(position));

             holder.icon.setImageResource(R.drawable.arraocha);







            return convertView;
        }

        class ViewHolder {
            ImageView icon;
            TextView text;



        }
}

Hope this helps.

StarsSky
  • 6,721
  • 6
  • 38
  • 63
Uday
  • 5,933
  • 9
  • 44
  • 76