Android ExpandableListview 如何用循環把 組和子元素展示出來
問題描述
以下代碼只是一個例子把組合子元素展示出來。 如果我連接去數據庫的話, 這種方法顯然行不通。因為我們無法確定有多少個組合多少個子。 大神們可否指點一下如何用 循環把它們展示出來? 例如用 For 循環。
adapter adapter; // BaseExpandableListAdapterExpandableListView expandableListView;List<String> category;HashMap<String,List<String>> item;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ex_listview); expandableListView=(ExpandableListView)findViewById(R.id.listview); display(); adapter=new adapter(this,category,item); expandableListView.setAdapter(adapter);}public void display(){ category=new ArrayList<String>(); item=new HashMap<String,List<String>>(); category.add('Western Food'); category.add('Chinese Food'); category.add('Japanese Food'); List<String> western_food = new ArrayList<String>(); western_food.add('Fried Chicken'); western_food.add('French Fries'); western_food.add('Beef Steak'); List<String> chinese_food = new ArrayList<String>(); chinese_food.add('Chicken Rice'); chinese_food.add('Duck Rice'); List<String> japanese_food = new ArrayList<String>(); japanese_food.add('Tapanyaki'); japanese_food.add('Takoyagi'); japanese_food.add('Sushi'); japanese_food.add('Lamian'); item.put(category.get(0), western_food); item.put(category.get(1), chinese_food); item.put(category.get(2), japanese_food);}
問題解答
回答1:無需知道有多少個組合與子,只要從數據庫中獲取的數據你能區別不同的組合與子所屬的組合就可以了。將數據放到相應的存儲集合中,在通過設計Adapter就可以展示。
相關文章:
1. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。2. javascript - [js]為什么畫布里不出現圖片呢?在線等3. python - 如何判斷爬蟲已經成功登陸?4. html - vue項目中用到了elementUI問題5. html5 - 有可以一次性把所有 css外部樣式轉為html標簽內style=" "的方法嗎?6. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變7. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?8. javascript - 有什么比較好的網頁版shell前端組件?9. javascript - 這不是對象字面量函數嗎?為什么要new初始化?10. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?
