java - Android:将 CheckBox 添加到 ListAdapter

标签 java android xml checkbox listactivity

我想知道如何为我的 ListActivity 的 ListAdapter 添加(和控制)复选框字段。这是迄今为止我用来设置它的代码:

@覆盖 公共(public)无效onCreate( bundle 保存实例状态){ super.onCreate(savedInstanceState); setContentView(R.layout.music);

      String[] proj = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Video.Media.SIZE };

      musicCursor = this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
      startManagingCursor(musicCursor);

        // THE DESIRED COLUMNS TO BE BOUND
      String[] columns = new String[] { MediaStore.Audio.Media.DISPLAY_NAME }; //, MediaStore.Audio.Media.SIZE
        // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
      int[] to = new int[] { R.id.text1 }; //, R.id.text2 , R.id.musicChecked

      mAdapter = new SimpleCursorAdapter(this, R.layout.song_item, musicCursor, columns, to);
      // SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER
      // i.e. the UI element
      setListAdapter(mAdapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
        Toast.makeText(this, "You selected: " + Integer.toString(position), Toast.LENGTH_LONG).show();

}

//音乐.xml

 <ListView android:id="@android:id/list"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"
           android:fastScrollEnabled="true"
           />

 <TextView android:id="@android:id/empty"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:text="No data"/>

//song_item.xml

 <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:paddingTop="5px"
     android:paddingBottom="5px">

     <CheckBox android:id="@+id/musicChecked"
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        />

     <TextView android:id="@+id/text1"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingTop="15px"
         android:paddingBottom="15px"/>

     </LinearLayout>

现在发生的事情是正确生成了列表,其中包含复选框和歌曲名称。但是,我的 onItemClick 不适用于添加的复选框“musicChecked”(但是,如果我从 XML 中删除它,我就会看到 Toast)。我希望能够单击一个字段并让复选框切换真/假。

感谢大家的帮助。

最佳答案

真的,这只是 catch 事件的问题。将复选框设置为

android:clickable="false" 
android:focusable="false" 

在您的 xml 布局中。然后只需使用继承的 setChecked() 方法处理现有 onListItemClick 方法中复选框的选择和取消选择即可。

关于java - Android:将 CheckBox 添加到 ListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6481153/

相关文章:

java - 如何找到一组的长度?

java - 在 glide 中处理 IllegalArgumentException ("You cannot start a load for a destroyed activity")的最佳方法是什么?

java - 服务应用程序停止工作

xml - R:如何获取站点时间的父属性和节点值?

xml - 复杂的 XML XSLT 查询,如 SQL 连接

XML 到缺少节点的数据框

java - 在 java 中填充值后,Excel 下载无法正常工作

Java while循环无法检测线程的变化

android - AppCompatActivity 给出错误

Android 平板电脑 API 和 Android 手机 API