1

i think ive tried everything(made focusable:false!!) and i cant capture in anyway the selected checkbox on my list item. even OnItemClickListener, doesnt respond to any click.

how can i retrieve checked checkboxes in my list item? my list item inclused: image view, 4 textviews and the checkbox

some code:

this is in my ListActivity class:

final String columns[] = new String[] { MyUsers.User._ID, 
        MyUsers.User.MSG, MyUsers.User.LOCATION }; 

int[] to = new int[] { R.id.toptext, R.id.bottomtext,R.id.ChkBox, R.id.Location};

Uri myUri = Uri.parse("content://com.idan.datastorageprovider/users"); 

Cursor cursor = getContentResolver().query(myUri, columns, null, null, null);   

            startManagingCursor(cursor);   


ListCursorAdapter myCursorAdapter=new ListCursorAdapter(this, 
        R.layout.listitem, cursor, columns, to); 

 this.setListAdapter(myCursorAdapter); 

and this is my Custom Cursor adapter class:

public class ListCursorAdapter extends SimpleCursorAdapter
{

 private Context context; 
 private int layout; 

 public ListCursorAdapter(Context context, int layout, Cursor c, 
    String[] from, int[] to) 
{ 
   super(context, layout, c, from, to); 
   this.context = context; 
   this.layout = layout; 

} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) 
{ 

   Cursor c = getCursor(); 

   final LayoutInflater inflater = LayoutInflater.from(context); 
   View v = inflater.inflate(layout, parent, false); 
           return v; 
} 

@Override
public void bindView(View v, Context context, Cursor c) 
{ 
    TextView topText = (TextView) v.findViewById(R.id.toptext); 
    if (topText != null) 
    { 
        topText.setText(""); 
    } 

    int nameCol = c.getColumnIndex(MyUsers.User.MSG); 
    String name = c.getString(nameCol); 
    TextView buttomTxt = (TextView) v.findViewById(R.id.bottomtext); 
    if (buttomTxt != null) 
    { 
        buttomTxt.setText("Message: "+name); 
    } 

nameCol = c.getColumnIndex(MyUsers.User.LOCATION); 
name = c.getString(nameCol); 
TextView location = (TextView) v.findViewById(R.id.Location); 
if (locationLinkTxt != null) 
{ 
    locationLinkTxt.setText(name); 
} 

I will appreciate any help! it's really depressing, I've tried many ways. many listeners, can't figure how to capture listener to my checkboxes.

The data I am binding to the list items from the database, isn't concered about the checkbox.. only the textviews.. there isn't any connection between the database and the checkbox I have in the list's item.

Thanks, idan.

<ImageView
    android:id="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginLeft="6dip"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:src="@drawable/icon" >
</ImageView>

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="1sp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/toptext"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:text="OrderNum" >
    </TextView>

    <TextView
        android:id="@+id/bottomtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="TweetMsg" >
    </TextView>

    <TextView
        android:id="@+id/twittLocation"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:singleLine="true"
        android:text="location" >
    </TextView>

    <TextView
        android:id="@+id/twittLocationlink"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="fill_horizontal"
        android:text="locationlink" >
    </TextView>
</LinearLayout>

<CheckBox
    android:id="@+id/deleteTwittChkBox"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="2dp"
    android:checked="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Delete" >
</CheckBox>

Maveňツ
  • 1
  • 12
  • 50
  • 89
rayman
  • 20,786
  • 45
  • 148
  • 246

3 Answers3

5

I would recommend you use Android's built-in support for multiple-choice lists (CHOICE_MODE_MULTIPLE).

The List11.java SDK sample demonstrates this. You can also find a project from one of my tutorials that uses it here.

You can still use this technique with your own layout, so long as you include a CheckedTextView with android:id="@android:id/text1" as shown in the android.R.layout.simple_list_item_multiple_choice resource, a copy of which ships with your SDK.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i`am not sure i understoond, what is the CheckedTextView connected o the idea of the checkbox listeners? ive tried to look some sample tutorials in that link, mybe u could give me a specific link for a specific tutorial? thank you, idan. – rayman Feb 14 '10 at 17:57
  • 1
    I am saying you should get rid of your "checkbox listeners". Design your UI around a standard multiple-choice `ListView`, where the user makes choices via the checkboxes, then you take action when the user indicates they are done (clicks the BACK button, clicks a "Next" `Button`, chooses a menu choice, etc.). – CommonsWare Feb 14 '10 at 18:03
  • Yes exaclly thats what i need in the scenario, ive got a button which deleting the items that have been checked, the prob is: how do i combine it with my Custom SimpleCurose.. or it's not connected to each other.. could you pass me a specific link to one of your tutorials? so i can follow it.. thanks again. } – rayman Feb 14 '10 at 18:05
  • so ive added those 3 lines as it seems in List11.java final ListView listView = getListView(); listView.setItemsCanFocus(false); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); but still how will i collect the items which thire checkboxes have been checked? thanks. – rayman Feb 14 '10 at 18:14
  • Call `getCheckedItemIds()` on your `ListView` – CommonsWare Feb 14 '10 at 18:39
  • okie i think i`am so closed to it to be done.. after ive done on a button press long [] items=listView.getCheckItemIds(); now ive selected in two items, the checkboxes they have in it. and the expression value of items after button click is: "items"= (id=830065282632) why doesnt it show me both of them..? thanks again. – rayman Feb 14 '10 at 18:55
  • If have no idea what "the expression value of items" means. – CommonsWare Feb 14 '10 at 19:11
  • i meant the value of the verb 'items' after executing items=listView.getCheckItemIds(); between ive edited my question and added the xml of row item. how should i adjust it? coz i got there two Linearlayouts. – rayman Feb 14 '10 at 19:21
  • Ive have examined the simple_list_item_multiple_choice.xml are you sure i can add more 'parts' to that example? i past my xml above, can i modify it and use it with the same logic? coz i need more parts in my list's item row, coz i`am binding data to each part in that row. – rayman Feb 14 '10 at 19:54
2

http://www.coderanch.com/t/513608/Android/Mobile/Multiple-Checkboxes-ListView

check this example it is very good example for custom listview with checkbox.

ranjit
  • 21
  • 1
0

I want also to select one item in the list (no CHOICE_MODE_MULTIPLE) and retrieve the states of the checkboxes. In my custom ListAdapter, I implemented something like :

    private void bindView(int position, View view) {
      final CheckBox myCheckBox = (CheckBox)view.findViewById(R.id.my_checkbox);

      final MyObject myModelObject = (MyObject)getItem(position);

      myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          myModelObject.setSelected(isChecked);
        }
    });

And in onItemClick() listener of the ListView, I can retrieve the checkboxes states with :

((MyObject)listView.getAdapter().getItem(n)).isSelected()
Yann
  • 3,841
  • 1
  • 22
  • 14