2011-01-31 94 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; 
     } 
    }; 
相關問題