简体   繁体   English

Android:将CheckBox添加到ListAdapter

[英]Android: adding CheckBox to a ListAdapter

I'm wondering how to add (and control) a CheckBox field for my ListActivity's ListAdapter. 我想知道如何为ListActivity的ListAdapter添加(和控制)CheckBox字段。 Here is my code thus far which I use to set it up: 到目前为止,这是我用来设置的代码:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @Override public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.music); 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();

}

//music.xml //music.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 //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>

Right now what happens is that the list is properly generated, with the checkboxes and the song names. 现在发生的事情是正确生成了列表,并带有复选框和歌曲名称。 However, my onItemClick doesn't work with the CheckBox "musicChecked" added in (however if I remove it from XML then I see the Toast). 但是,我的onItemClick不能与添加的CheckBox“ musicChecked”一起使用(但是,如果我从XML中删除它,那么我会看到Toast)。 I want to be able to click on a field and have the CheckBox toggle true/false. 我希望能够单击一个字段并使CheckBox切换为true / false。

Thanks for any help guys. 感谢您的帮助。

Really, it's just a matter of catching the event. 确实,这只是赶上活动的问题。 Set your checkbox to 将您的复选框设置为

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

in your xml layout. 在您的xml布局中。 Then just handle the selecting and unselecting of the checkbox in your existing onListItemClick method with the inherited setChecked() method. 然后只需使用继承的setChecked()方法处理现有onListItemClick方法中复选框的选择和取消选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM