【问题标题】:Checkbox in listview with Custom SimpleCurser binding具有自定义 SimpleCurser 绑定的列表视图中的复选框
【发布时间】:2011-01-16 17:27:13
【问题描述】:

我想我什么都试过了(变得可聚焦:假!!) 而且我无论如何都无法捕获列表项上选定的复选框。 即使是 OnItemClickListener,也不响应任何点击。

如何在我的列表项中检索选中的复选框? 我的列表项包括:图像视图、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); 
} 

我将不胜感激!真的很郁闷,试过很多方法。许多听众,不知道如何将听众捕捉到我的复选框。

我从数据库绑定到列表项的数据,不关心复选框..只有文本视图..数据库和我在列表项中的复选框之间没有任何连接。

谢谢, 伊丹

<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>

【问题讨论】:

    标签: android


    【解决方案1】:

    我建议您使用 Android 对多项选择列表的内置支持 (CHOICE_MODE_MULTIPLE)。

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

    您仍然可以将此技术用于您自己的布局,只要您在 android:id="@android:id/text1" 中包含一个 CheckedTextView,如 android.R.layout.simple_list_item_multiple_choice 资源中所示,您的 SDK 附带了该资源的副本。

    【讨论】:

    • 我不确定我是否理解,CheckedTextView 连接到复选框侦听器的想法是什么?我试图在该链接中查看一些示例教程,你可以给我一个特定教程的特定链接吗?谢谢你,伊丹。
    • 我是说你应该摆脱你的“复选框监听器”。围绕标准的多项选择ListView 设计您的 UI,用户通过复选框进行选择,然后在用户指示完成时采取行动(单击“返回”按钮,单击“下一步”Button,选择菜单选择等)。
    • 是的,这正是我在场景中需要的,我有一个按钮可以删除已检查的项目,问题是:我如何将它与我的自定义 SimpleCurose 结合使用.. 或者它没有连接彼此..你能给我一个具体的链接到你的一个教程吗?所以我可以关注它.. 再次感谢。 }
    • 所以我添加了这 3 行,因为它看起来在 List11.java final ListView listView = getListView(); listView.setItemsCanFocus(false); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);但我仍将如何收集已选中三个复选框的项目?谢谢。
    • 在您的ListView 上致电getCheckedItemIds()
    【解决方案2】:

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

    检查这个例子,它是带有复选框的自定义列表视图的一个很好的例子。

    【讨论】:

      【解决方案3】:

      我还想在列表中选择一项(无 CHOICE_MODE_MULTIPLE)并检索复选框的状态。在我的自定义 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        相关资源
        最近更新 更多