views:

658

answers:

4

hello guys,

i have a question, been stuck for a while, i dont know how can i add a checkbox in the list, for example if I have a list of items i want to be able to check them. my xml code is the following:

<LinearLayout android:id="@+id/topLayout"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_centerHorizontal="true"  android:layout_height="wrap_content">

</LinearLayout>

<LinearLayout
    android:id="@+id/middleLayout"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <LinearLayout android:id="@+id/leftMiddleLayout"
            android:orientation="vertical" android:layout_below="@+id/topLayout"
            android:layout_above="@+id/bottomLayout"
            android:layout_width="60px" android:layout_height="wrap_content"
            >

            <ListView android:id="@+id/checkboxList" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" ></ListView>

            <CheckBox android:id="@+id/checkbox"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:checked="false"
            android:text="test">
            </CheckBox>

    </LinearLayout>

    <LinearLayout android:id="@+id/rightMiddleLayout"
            android:orientation="vertical" android:layout_below="@+id/topLayout"
            android:layout_above="@+id/bottomLayout"
            android:layout_width="280px" android:layout_height="wrap_content"
            >

            <ListView android:id="@+id/list" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" ></ListView>

            <TextView android:id="@+id/text" android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>                   
    </LinearLayout>
</LinearLayout>

<LinearLayout android:id="@+id/bottomLayout" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingBottom="5pt"
    >

    <EditText android:id="@+id/insertNewItem"
        android:layout_width="220px" android:layout_height="wrap_content" />

    <TextView android:layout_width="10px" android:layout_height="wrap_content" />

    <Button android:id="@+id/addItemButton" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Add Item"/>
</LinearLayout>

if you have any ideas please let me know, its for my academic studies :((

Thank you!

A: 

If you use a ListAdapter (e.g. a customized BaseAdapter) on your listView with setAdapter() you can use LayoutInflater inside your adapter's getView() to supply any layout you like for your list entries. This can include a CheckBox.

Jim Blackler
trying it now thanks
Bugzy bug
OMG, I cannot solve this problem... it's been ages i am stuck on it :(((
Bugzy bug
A: 

Or you can extends any ListAdapter to a subclass and override bindView. Inside of if you would set .setText for the ChecBoxes!

Philipz
oh, thanks!do you know where to find an example of that?the thing is i am reading the items from the database, using JSON, so i have items. i set a ListAdapter for that, i can see the items. the problem is i cannot find a way to put a checkbox next to each item :(
Bugzy bug
+1  A: 

I recommend you watch this video from the android dev site on how to Make your Android UI Fast and Efficient. It will give you code to solve your problem and show you the proper way to implement your adapter to make sure it's as fast as can be.

Dave.B
thank you very much!!
Bugzy bug
A: 
public class myAdapter extends SimpleCursorAdapter {
    public myAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to);  
}

@Override   
public void bindView(View view, Context context, Cursor cursor) {
    cb=(CheckBox)view.findViewById(R.id.cb);
    cb.setText(dbgetStringValue);
    cbText=(TextView)view.findViewById(R.id.cbText);
            cbText.setText(dbgetStringValue);
    cb.setChecked(false);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton cb, boolean isChecked) {            
            if(cb.isChecked()) {                    
                // action
            }
            else if(isChecked==false) {
                // action
            }
        }           
    });
}
} 

is that what you want?

Philipz