我是Android新手,正在开发一个活动应用。该应用有一个子事件,因此我使用了可扩展的列表视图,但其顺序与Arraylist对象顺序不同。我已经使用了sort函数&它的工作,但我需要订购我的订单。
如何解决这个问题?
DataPump.java
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData(ArrayList<Guide> guides) {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
List<String> parent = new ArrayList<String>();
for (int i = 0; i < guides.size(); i++) {
parent=new ArrayList<>();
Guide guide=guides.get(i);
// Log.e("size***",guide.getName()+"-"+ String.valueOf(guide.getItem_list().size()));
if (guide.getItem_list().isEmpty()){
}else{
for (int j=0;j<guide.getItem_list().size();j++){
parent.add(guide.getItem_list().get(j).getName());
// Log.e("add****", String.valueOf(guides.get(i).getItem_list().get(j).getName()));
}
}
expandableListDetail.put(guide.getName(), parent);
}
在Activity类中,
expandableListView = (ExpandableListView) findViewById(R.id.exLv_events);
expandableListDetail = ExpandableListDataPump.getData(guideArrayList);
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
// Collections.sort(expandableListTitle);
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
答案 0 :(得分:0)
我将HashMap更改为LinkedHashMap,然后问题解决了,
代码段, datapump.java
public class ExpandableListDataPump {
public static LinkedHashMap<String, List<String>> getData(ArrayList<Guide> guides) {
LinkedHashMap<String, List<String>> expandableListDetail = new LinkedHashMap<>();
List<String> parent = new ArrayList<String>();
for (int i = 0; i < guides.size(); i++) {
parent=new ArrayList<>();
Guide guide=guides.get(i);
Log.e("dpump***",guide.getName());
if (guide.getItem_list().isEmpty()){
}else{
for (int j=0;j<guide.getItem_list().size();j++){
parent.add(guide.getItem_list().get(j).getName());
// Log.e("add****", String.valueOf(guides.get(i).getItem_list().get(j).getName()));
}
}
// expandableListDetail.put(guide.getName(), parent);
expandableListDetail.put(guide.getName(), parent);
Log.e("size###", String.valueOf(expandableListDetail.size()));
}
return expandableListDetail;
}
}
和活动
LinkedHashMap的&GT; expandableListDetail;
加载时
expandableListView = (ExpandableListView) findViewById(R.id.exLv_events);
expandableListDetail = ExpandableListDataPump.getData(guideArrayList);
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);