2011-01-31 95 views
1

我有一个android.R.layout.simple_list_item_multiple_choice复选框,想要启动其中的一些。 我该怎么做? 我有以下代码:Android:如何设置列表项检查?

private void fillList() { 
    Cursor NotesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(NotesCursor); 

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY, NotesDbAdapter.KEY_CHECKED }; 

    int[] to = new int[] { 
    android.R.id.text1, 
    android.R.id.text2, 
    //How set checked or not checked? 
    }; 

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, NotesCursor, 
      from, to); 
    setListAdapter(notes); 

} 
+0

你见过这样的:http://developer.android.com/reference /android/widget/CheckBox.html 不帮你吗? – gnclmorais 2011-01-31 15:52:44

回答

2
  1. 把你的复选框的资源ID在你排布置成to数组,对应NotesDbAdapter.KEY_CHECKED光标from阵列。

  2. 执行SimpleCursorAdapter.ViewBinder

  3. 是否有ViewBinder.setViewValue()方法检查其何时调用NotesDbAdapter.KEY_CHECKED列。

  4. 当它是而不是 KEY_CHECKED列时,让它返回false适配器将执行通常的操作。

  5. 当它是KEY_CHECKED列时,让它将CheckBox视图(需要强制转换)设置为选中或不选,然后返回true,以便适配器不会尝试自己绑定它。光标和相应的列ID可用于访问查询数据,以确定是否检查复选框。

  6. 通过setViewBinder()

设置你的ViewBinder在SimpleCursorAdapter这里是我的ViewBinder实现之一。它不是checboxes,而其做一个文本视图的一些别致的格式,但它应该给你一些想法的方法:

private final SimpleCursorAdapter.ViewBinder mViewBinder = 
    new SimpleCursorAdapter.ViewBinder() { 
     @Override 
     public boolean setViewValue(
       final View view, 
       final Cursor cursor, 
       final int columnIndex) { 
      final int latitudeColumnIndex = 
       cursor.getColumnIndexOrThrow(
         LocationDbAdapter.KEY_LATITUDE); 
      final int addressStreet1ColumnIndex = 
       cursor.getColumnIndexOrThrow(
         LocationDbAdapter.KEY_ADDRESS_STREET1); 

      if (columnIndex == latitudeColumnIndex) { 

       final String text = formatCoordinates(cursor); 
       ((TextView) view).setText(text); 
       return true; 

      } else if (columnIndex == addressStreet1ColumnIndex) { 

       final String text = formatAddress(cursor); 
       ((TextView) view).setText(text); 
       return true; 

      } 

      return false; 
     } 
    }; 
相关问题