开发者

Android: adding CheckBox to a ListAdapter

I'm wondering how to add (and control) a CheckBox field for my ListActivity's List开发者_Python百科Adapter. Here is my code thus far which I use to set it up:

@Override public void 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();

}

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

 <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). I want to be able to click on a field and have the CheckBox toggle 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. Then just handle the selecting and unselecting of the checkbox in your existing onListItemClick method with the inherited setChecked() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜