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. python - 请问用cxfreeze打包的可执行程序能反编译出源码么?2. javascript - 这种上传图片预览怎么做?3. javascript - prismplayer播放器全屏问题4. javascript - vue2.0中router.repalce跟router.push有什么区别,二者的应用场景是会有怎样的区别?5. javascript - js判断一个数组是否重复6. javascript - input输入框触发change事件后,外部元素会被选中7. python - 用__call__ 实现装饰器功能8. javascript - 把字符串日期转换成一般时间格式后,发现小于10的数字不会自动添加0在前面,如下9. python相关问题求解决,有偿10. javascript - 在sublime中如何给.vue文件注释?