0

I have a listview with each item containing an image, a textview and a checkbox. I am trying to have it so wherever you click within the item it highlights the checkbox. As stated in this response I set the focusable attribute of the checkbox to false, but I am still only able to click the checkbox to change its state. What's even more interesting is when I click on a checkbox every proceeding 10th item is also highlighted. Any suggestions?

Here is my xml for an item.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image"
        android:layout_width="50dip"
        android:layout_height="50dip" 
        android:src="@drawable/stub"   
        android:scaleType="centerCrop"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:layout_gravity="left|center_vertical" 
        android:textSize="20dip" 
        android:layout_marginLeft="10dip"/>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"/>

</LinearLayout>

Main xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="Clear Cache"/>
</LinearLayout>
Community
  • 1
  • 1
James Fazio
  • 6,370
  • 9
  • 38
  • 47
  • This one looks similar to yours: http://stackoverflow.com/questions/5925119/every-seventh-box-checked-with-checkboxes-in-listview – blacharnia May 06 '12 at 07:39

3 Answers3

0

in the main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:layout_marginTop="5dp">

    <RelativeLayout
        android:layout_width="450dp" 
        android:layout_height="25dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp">

     <ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:layout_weight="1"/>
     </RelativeLayout>



     <RelativeLayout
        android:layout_width="450dp" 
        android:layout_height="25dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp">

     <Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:text="Clear Cache"/>
     </RelativeLayout>

</LinearLayout>

Hope this help you...

JLouis
  • 284
  • 1
  • 5
  • 18
0

in the process of populating the list, you can set a OnItemClickListener and change the checkbox to checked. try something like this:

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ListItem item = (ListItem) adapter.getItem(position);
                if(item.checkbox.isChecked()) 
                      item.checkbox.setChecked(false);
                else
                      item.checkbox.setChecked(true);
            }
 });
thepoosh
  • 12,497
  • 15
  • 73
  • 132
0
  1. create a CheckableLinearLayout or CheckableRelativeLayout for your ListView's item layout container,
  2. set your listview's mode to 'multipleChoice'

find more details in the links below:

Longerian
  • 723
  • 5
  • 13