// Adapter构造方法中对hashMap实例化 hashMap = new HashMap<Integer, String>(); for (int i = 0; i < buildings; i++) { hashMap.put(buildingListEntities.get(i).getBuildingId(), buildingListEntities.get(i).getBuildingName()); }
// Adapter的getHeaderView中取值 for (Map.Entry<Integer, String> entry : hashMap.entrySet()) { if (floorListEntities.get(position).getBuildingId() == entry.getKey()) { holder.text.setText(entry.getValue()); } }
修改之后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
SparseArray<String> stringSparseArray;
// Adapter构造方法中对stringSparseArray实例化 stringSparseArray = new SparseArray<>(); for (int i = 0; i < buildings; i++) { stringSparseArray.put(buildingListEntities.get(i).getBuildingId(), buildingListEntities.get(i).getBuildingName()); }
// Adapter的getHeaderView中取值 for (int i = 0; i < stringSparseArray.size(); i++) { int key = stringSparseArray.keyAt(i); if (floorListEntities.get(position).getBuildingId() == key) { holder.text.setText(stringSparseArray.get(key)); } }