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?

4
  • 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. Commented Mar 24, 2011 at 9:58
  • Look at the code here You can select multiple items in listview. Check here With Text and Image example : here Commented Mar 24, 2011 at 9: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. Commented Sep 8, 2013 at 10:54
  • This is not a duplicate... This is about ListView + CheckBox,.. and not about data binding! Dumb moderators...!!!! Commented Apr 25, 2015 at 9:21

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
how to allow stealing of click on checkbox on item click of list view android?
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
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);    

1 Comment

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.
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());


                }
        });

Comments

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>

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.