【问题标题】:Android: adding CheckBox to a ListAdapterAndroid:将 CheckBox 添加到 ListAdapter
【发布时间】:2011-09-22 19:04:48
【问题描述】:

我想知道如何为我的 ListActivity 的 ListAdapter 添加(和控制)一个 CheckBox 字段。到目前为止,这是我用来设置它的代码:

@覆盖 公共无效 onCreate(Bundle savedInstanceState) { 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 不适用于添加的 CheckBox“musicChecked”(但是,如果我从 XML 中删除它,那么我会看到 Toast)。我希望能够单击一个字段并让 CheckBox 切换真/假。

感谢大家的帮助。

【问题讨论】:

    标签: java android xml checkbox listactivity


    【解决方案1】:

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

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

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

    【讨论】:

    • 好吧,所以我将可点击的复选框设置为 false,但由于某种原因我仍然没有捕捉到该事件。你怎么看?
    • 对不起!我忘了提到你还需要在你的 xml 中设置 android:focusable="false" 。否则,复选框会从列表项中获取焦点。我更新了我的答案以反映这一点。
    猜你喜欢
    • 2011-05-18
    • 2016-03-02
    • 1970-01-01
    • 2013-07-06
    • 2023-04-02
    • 1970-01-01
    • 2011-02-10
    • 2018-06-22
    • 2021-10-28
    相关资源
    最近更新 更多