在下面的代码中,我将逐个加载一个包含数据的ArrayList。
此ArrayList用于ExpandableListView。所以它包含群组和孩子。
每个组都有一个名称和一个描述。
每个孩子都有一个名字。
有些团体没有孩子。
public class CategoriesExpandableList extends Activity implements OnChildClickListener,
OnGroupClickListener, OnGroupExpandListener, OnGroupCollapseListener {
private static Context mContext;
private ExpandListAdapter ExpAdapter=null;
private ArrayList<ExpandListGroup> ExpListItems;
ArrayList<ExpandListGroup> list=null;
private ExpandableListView ExpandList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.categoriesexpandable);
ExpandList = (ExpandableListView) findViewById(R.id.ExpList);
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapter(CategoriesExpandableList.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
ExpandList.setOnGroupClickListener(this);
ExpandList.setOnChildClickListener(this);
mContext=this;
}
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru0 = new ExpandListGroup();
gru0.setName("My Favorites");
gru0.setDescription("My favourite points of interest");
gru0.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru1 = new ExpandListGroup();
gru1.setName("Essentials");
gru1.setDescription("Very essential points of interest");
gru1.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru2 = new ExpandListGroup();
gru2.setName("Sights");
gru2.setDescription("Beautiful sights you have to visit");
gru2.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru3 = new ExpandListGroup();
gru3.setName("Beaches");
gru3.setDescription("Beaches near Agios Nikolaos");
ExpandListChild ch3_0 = new ExpandListChild();
ch3_0.setName("In town");
ch3_0.setTag(null);
list2.add(ch3_0);
ExpandListChild ch3_1 = new ExpandListChild();
ch3_1.setName("Outside town");
ch3_1.setTag(null);
list2.add(ch3_1);
gru3.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
list.add(gru0);
list.add(gru1);
list.add(gru2);
list.add(gru3);
return list;
}
}
我想知道是否有一种快速方法可以在ArrayList中加载这些数据。例如,通过调用这样的方法:
addPoint(String groupname, String groupdescription, String child01)
addPoint("Banks", "Banks and ATMs", "Only banks")
addPoint("Banks", "Banks and ATMs", "Only ATMs")
// Here i have only a group and no children...
addPoint("Sights", "Beautiful sights to visit", "")
答案 0 :(得分:0)
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
String[][] expandablelist={ {"My Favorites", "My favourite points of interest","","","","","","","","","",""},
{"Essentials", "Very essential points of interest","","","","","","","","","",""},
{"Sights", "Beautiful sights you have to visit","","","","","","","","","",""},
{"Beaches", "Nice beaches","In town","Around town","","","","","","","",""},
{"Banks", "Banks and ATMs","","","","","","","","","",""},
{"Pharmacies", "All pharmacies near you","","","","","","","","","",""},
{"Doctors", "Doctors in case of emergency","Pediatricians","Pathologists","General doctors","Eye doctors","Surgeons","Otorhinolayngologists","Orthopaedists","Gynecologists","Dermatologists","Cardiologists"}
};
for(int i=0; i<=6; i++) {
System.out.println("i= "+i);
ExpandListGroup gru = new ExpandListGroup();
gru.setName(expandablelist[i][0]);
gru.setDescription(expandablelist[i][1]);
for(int j=2; j<=11; j++) {
System.out.println("j= "+j);
System.out.println("length= "+expandablelist[i][j].length());
if(expandablelist[i][j].length()!=0) {
ExpandListChild chld = new ExpandListChild();
chld.setName(expandablelist[i][j]);
chld.setTag(null);
list2.add(chld);
}
}
if(list2!=null) {
gru.setItems(list2);
}
list2 = new ArrayList<ExpandListChild>();
list.add(gru);
}
}
我已经找到了一种从数组中加载ArrayList的方法,就像你在上面的代码中看到的那样。 感谢您阅读我的问题。 我相信有一个更好的解决方案,如果我有一些时间,我会试着弄明白。