2012-10-06 87 views
2

我想使用數據庫支持的ExpandableListView,根據數據庫字段,子項應顯示在兩個不同視圖之一中。我可以使用SimpleCursorTreeAdapter爲ExpandableListView中的孩子使用不同的視圖嗎?

(如果一個項目有「做」在數據庫中設置的標誌,應該顯示爲灰色,否則應顯示爲白色。)

我這是怎麼了ExpandableListView綁定到數據庫:

dbAdapter = new TasksDbAdapter(this); 
dbAdapter.open(); 
c = dbAdapter.getTaskGroupsInList(listId); 
startManagingCursor(c); 
    dbTreeAdapter = new TaskListTreeAdapter(
      getApplicationContext(), 
      c, 
      listId, 
      R.layout.listgroupitem, 
      R.layout.listitem, 
      new String[] { 
       TaskDbAdapter.KEY_TASKTYPE_NAME }, 
      new int[] { 
       R.id.groupName }, 
      new String[] { 
       TaskDbAdapter.KEY_LISTITEMS_AMOUNT, 
       TaskDbAdapter.KEY_UNITS_NAME, 
       TaskDbAdapter.KEY_TASK_NAME }, 
      new int[] { 
       R.id.tvItemAmount, 
       R.id.tvItemUnit, 
       R.id.tvItemName }); 

這是適配器的getChildView方法:

public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     View vReturnView = null; 
     Cursor cGroupsTemp = dbAdapter.getGroups(listId); 
     if (cGroupsTemp.moveToPosition(groupPosition)) 
     { 
      long groupId = cGroupsTemp.getLong(cGroupsTemp.getColumnIndex(KEY_PRODTYPE_ID)); 
      Cursor cChildsTemp = dbAdapter.getChildren(listId, groupId); 
      if (cChildsTemp.moveToPosition(childPosition)) 
      { 
       long childId = cChildsTemp.getLong(cChildsTemp.getColumnIndex(KEY_LISTITEMS_ID)); 
       boolean bought = cChildsTemp.getInt(cChildsTemp.getColumnIndex(KEY_DONE)) > 0; 
       LayoutInflater inflater = getLayoutInflater(); 
       if (bought) 
       { 
        vReturnView = inflater.inflate(R.layout.listenlistitemdone, null); 
       } else { 
        vReturnView = inflater.inflate(R.layout.listenlistitem, null); 
       } 
      } 
     } 
     return vReturnView; 
    } 

不幸的是,兩種不同的看法是不正確顯示。 有誰知道爲什麼? 這兩個視圖必須屬於一個普通的ViewGroup嗎? 我沒有在Adapter中找到像getViewTypeCount()或getItemViewType()這樣的方法。 這個適配器不需要它們嗎? 兩個視圖中的元素是否必須具有相同的ID(因爲適配器僅將一個ID綁定到數據庫列)?

回答

0

我不相信你能做到這一點,因爲(你猜)佈局必須有重複ID,我不認爲這是允許的。

而不是試圖使用兩種不同的佈局,爲什麼你不使用一個單一的,然後從ExpandableListAdapter重寫bindChildView並改變那裏的顏色?

從我的項目之一的少量樣品(編輯以它凝結到一個可管理塊):

protected void bindChildView(View view, Context context, Cursor cursor, 
      boolean isLastChild) { 

     TextView name = (TextView) view.findViewById(R.id.ListItem1); 
     TextView qty = (TextView) view.findViewById(R.id.ListItem3); 

     name.setTextColor(GroceryApplication.shoplistitem_name); 
     qty.setTextColor(GroceryApplication.shoplistitem_qty); 

     name.setText(cursor.getString(2)); 
     qty.setText(cursor.getString(1)); 

     if (cursor.getInt(5) == 1) { 
      name.setPaintFlags(name.getPaintFlags() 
        | Paint.STRIKE_THRU_TEXT_FLAG); 
      qty.setPaintFlags(qty.getPaintFlags() 
        | Paint.STRIKE_THRU_TEXT_FLAG); 
      view.setBackgroundResource(R.color.purchased); 
     } else { 
      name.setPaintFlags(name.getPaintFlags() 
        & ~Paint.STRIKE_THRU_TEXT_FLAG); 
      qty.setPaintFlags(qty.getPaintFlags() 
        & ~Paint.STRIKE_THRU_TEXT_FLAG); 
      view.setBackgroundResource(R.color.black); 
     } 
    } 
} 

上面的代碼使用在分貝的標記,這將導致該應用要麼改變的背景灰色和文本是刪除線樣式或將背景色設置爲我的默認顏色並取消刪除線文本。

相關問題