这里有两个问题。按照教程,我需要一点帮助使其适应我的项目。 1)每组的孩子都是一样的。例如,Arraylist餐厅包含“Dining Commons”组的孩子们,我需要再制作两个包含学术建筑和住宅建筑的arraylists,并且需要成为他们各自父母的孩子。
2)是否可以使子项目可点击?使用clicklistener或类似的东西。
java source。
package com.bogotobogo.android.smplexpandable;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Demonstrates expandable lists backed by a Simple Map-based adapter
*/
public class SmplExpandable extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
ArrayList buildings = BuildingList();
ArrayList diningCommonBuildings = DiningList();
private ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < buildings.size(); i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, (String) buildings.get(i));
//curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> dining = new ArrayList<Map<String, String>>();
for (int j = 0; j < diningCommonBuildings.size(); j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
dining.add(curChildMap);
curChildMap.put(NAME, (String) diningCommonBuildings.get(j));
//curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(dining);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
private ArrayList BuildingList() {
ArrayList buildings = new ArrayList();
buildings.add("Academic Buildings");
buildings.add("Dining Commons");
buildings.add("Residential Buildings");
return buildings;
}
private ArrayList DiningList() {
ArrayList dining = new ArrayList();
dining.add("Berkshire");
dining.add("Franklin");
dining.add("Hampden");
dining.add("Hampshire");
dining.add("Worcester");
return dining;
}
}
答案 0 :(得分:1)
看看这个例子,
public class MainExpand extends ExpandableListActivity {
private ExpandableListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> parent = new ArrayList<Map<String,String>>();
List<List<Map<String, String>>> childGroup = new ArrayList<List<Map<String,String>>>();
for (int i = 0; i < 5; i++) {
Map<String, String> parentMap = new HashMap<String, String>();
parentMap.put("lalit", "Parent "+i);
parent.add(parentMap);
List<Map<String, String>> child = new ArrayList<Map<String,String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> childMap = new HashMap<String, String>();
childMap.put("lalit", "Child "+j);
child.add(childMap);
}
childGroup.add(child);
}
adapter = new SimpleExpandableListAdapter(
this,
parent,android.R.layout.simple_expandable_list_item_1,new String[] {"lalit"},new int[] { android.R.id.text1, android.R.id.text2},
childGroup,android.R.layout.simple_expandable_list_item_2,new String[]{"lalit","even"}, new int[]{android.R.id.text1,android.R.id.text2}
);
setListAdapter(adapter);
}
答案 1 :(得分:1)
我结合使用了你的两个答案。谢谢。我利用了多维数组。首先我创建了这些数组。
private String[] buildingTypes = {
"Academic Buildings", "Dining Commons", "Residential Halls" };
private String[][] buildingList = {
{ "Agriculutural Engineering", "Army ROTC", "Arnold House", "Studio Arts Bldg","Bartlett" },
{ "Berkshire", "Franklin", "Hampden", "Hampshire", "Worcester" },
{ "Baker Hall", "Brett Hall", "Brooks Hall", "Brown Hall","Butterfield Hall" },
然后我修改了for循环
for (int i = 0; i < buildingTypes.length; i++) {
Map<String, String> parentMap = new HashMap<String, String>();
parentMap.put("lalit", buildingTypes[i]);
parent.add(parentMap);
List<Map<String, String>> child = new ArrayList<Map<String,String>>();
for (int j = 0; j < rows; j++) {
Map<String, String> childMap = new HashMap<String, String>();
childMap.put("lalit", buildingList[i][j]);
child.add(childMap);
}
childGroup.add(child);
}
答案 2 :(得分:0)
以下是您的两个问题的答案。如果有什么不合适,请在评论中告诉我。
第1部分答案:
private String[][] children = {
{ "Academic1", "Academic2", "Academic3", "Academic4" },
{ "Dining1", "Dining2", "Dining2", "Dining3" },
{ "Residential1", "Residential2" },
};
第2部分的答案):
http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html
onChildClick(ExpandableListView parent,View v,int groupPosition,int childPosition,long id)子进程中要调用的回调方法 此可扩展列表已被点击。