以下代码只是一个例子把组合子元素展示出来。 如果我连接去数据库的话, 这种方法显然行不通。因为我们无法确定有多少个组合多少个子。 大神们可否指点一下如何用 循环把它们展示出来? 例如用 For 循环。
adapter adapter; // BaseExpandableListAdapter
ExpandableListView expandableListView;
List<String> category;
HashMap<String,List<String>> item;
@Override
protected 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);
}
You don’t need to know how many combinations and sub-groups there are, as long as you can distinguish the combinations to which different combinations and sub-groups belong from the data obtained from the database. Put the data into the corresponding storage collection and display it through the design
Adapter
.