这是您想要的简单示例,它将消除您的所有疑问..
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
public class SimpleExpandableListExampleActivity extends ExpandableListActivity {
private static final String NAME = "NAME";
private ExpandableListAdapter mAdapter;
private String group[] = {"Development" , "Data Process Team"};
private String[][] child = { { "John", "Bill" }, { "Alice", "David" } };
@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 < group.length; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, group[i]);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < child[i].length; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, child[i][j]);
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(this, groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME }, new int[] { android.R.id.text1 },
childData, android.R.layout.simple_expandable_list_item_2,
new String[] { NAME }, new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
}
}
你所要做的就是
- 创建一个 Android 项目,并将其
SimpleExpandableListExampleActivity
作为您的主要活动..
- 复制粘贴该活动中给出的代码。
- 就是这样..运行你的代码...
希望这可以帮助...