Android 具有自定义SimpleCurser绑定的listview中的复选框

Android 具有自定义SimpleCurser绑定的listview中的复选框,android,Android,我想我什么都试过了(调焦:错!!) 我无论如何都无法捕获列表项上的选中复选框。 即使是McClickListener,也不会响应任何单击 如何检索列表项中的复选框? 我的列表项包括:图像视图、4个文本视图和复选框 一些代码: 这在我的ListActivity类中: final String columns[] = new String[] { MyUsers.User._ID, MyUsers.User.MSG, MyUsers.User.LOCATION }; int[]

我想我什么都试过了(调焦:错!!) 我无论如何都无法捕获列表项上的选中复选框。 即使是McClickListener,也不会响应任何单击

如何检索列表项中的复选框? 我的列表项包括:图像视图、4个文本视图和复选框

一些代码:

这在我的ListActivity类中:

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); 
这是我的自定义游标适配器类:

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); 
} 
我将感谢任何帮助!真令人沮丧,我试过很多方法。许多监听器都不知道如何将监听器捕获到我的复选框中

我绑定到数据库列表项的数据与复选框无关。。只有文本视图。。数据库和列表项中的复选框之间没有任何连接

谢谢, 伊丹



我建议您使用Android内置的多选列表支持(
choice\u MODE\u multiple

SDK示例演示了这一点。您还可以从我的教程中找到一个使用它的项目


您仍然可以在自己的布局中使用此技术,只要您在
android:id=“@android:id/text1”
中包含一个
CheckedTextView
,如
android.R.layout.simple\u list\u item\u多选资源中所示,SDK附带的副本。

我建议您使用Android内置的多选列表支持(
choice\u MODE\u multiple

SDK示例演示了这一点。您还可以从我的教程中找到一个使用它的项目


您仍然可以在自己的布局中使用此技术,只要您在
android:id=“@android:id/text1”
中包含一个
CheckedTextView
,如
android.R.layout.simple\u list\u item\u多选
资源,该资源的副本随SDK一起提供。

我还想在列表中选择一项(无选择模式)并检索复选框的状态。在我的自定义ListAdapter中,我实现了如下功能:

    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);
        }
    });
在ListView的onItemClick()监听器中,我可以通过以下方式检索复选框状态:

((MyObject)listView.getAdapter().getItem(n)).isSelected()

我还想在列表中选择一项(无选择模式多个)并检索复选框的状态。在我的自定义ListAdapter中,我实现了如下功能:

    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);
        }
    });
在ListView的onItemClick()监听器中,我可以通过以下方式检索复选框状态:

((MyObject)listView.getAdapter().getItem(n)).isSelected()

选中此示例这是使用复选框自定义listview的很好示例。


选中此示例这是一个非常好的示例,用于使用复选框自定义listview。

我不确定我是否了解,CheckedTextView与复选框侦听器的想法有什么联系?我尝试在该链接中查看一些示例教程,mybe你可以给我一个特定教程的特定链接吗?谢谢你,idan。我是说你应该t去掉“复选框侦听器”。围绕标准的多选项
列表视图设计UI,用户通过复选框进行选择,然后在用户指示完成时采取行动(单击“上一步”按钮、单击“下一步”
按钮、选择菜单选项等)是的,这就是我在场景中需要的,我有一个按钮,可以删除已检查的项目,问题是:我如何将它与我的自定义SimpleCrose结合起来..或者它彼此没有连接..你能给我一个指向你的教程的特定链接吗?这样我就可以跟着它了..再次感谢..所以我添加了这3行代码,因为它已经结束了List11.java final ListView ListView=getListView();ListView.setItemsCanFocus(false);ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);但我如何收集已选中三个复选框的项目?谢谢。调用
GetCheckEdItemId()
在你的
列表视图上
我不确定我是否了解,CheckedTextView与复选框侦听器的概念有什么联系?我尝试在该链接中查看一些示例教程,mybe你可以给我一个特定教程的特定链接吗?谢谢你,idan。我是说你应该摆脱你的“复选框侦听器”。围绕一个标准的多选
列表视图设计用户界面,其中用户通过复选框进行选择,然后在用户指示已完成时采取行动(单击“上一步”按钮、单击“下一步”
、选择菜单选项等)是的,这就是我在场景中需要的,我有一个按钮,可以删除已检查的项目,问题是:我如何将它与我的自定义SimpleCrose结合起来..或者它彼此没有连接..你能给我一个指向你的教程的特定链接吗?这样我就可以跟着它了..再次感谢..所以我添加了这3行代码,因为它已经结束了List11.java final ListView ListView=getListView();ListView.setItemsCanFocus(false);ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);但我如何收集已选中三个复选框的项目?谢谢。请在
ListView上调用
getCheckedItemIds()