您的位置:首页技术文章
文章详情页

Android ExpandableListview 如何用循环把 组和子元素展示出来

【字号: 日期:2024-08-30 18:33:43浏览:55作者:猪猪

问题描述

以下代码只是一个例子把组合子元素展示出来。 如果我连接去数据库的话, 这种方法显然行不通。因为我们无法确定有多少个组合多少个子。 大神们可否指点一下如何用 循环把它们展示出来? 例如用 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);}

Android ExpandableListview 如何用循环把 组和子元素展示出来

问题解答

回答1:

无需知道有多少个组合与子,只要从数据库中获取的数据你能区别不同的组合与子所属的组合就可以了。将数据放到相应的存储集合中,在通过设计Adapter就可以展示。

相关文章: