我有ArrayList
>>它包含某些键值条目。如: -
ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("NewId", newId);
map.put("Title", title);
map.put("Description", description);
myList.add(map);
“NewId”对于多个条目可能类似。
我也有一系列颜色: -
String[] colors = new String[]{"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};
我想要一个mew组,其中所有条目都有相同的“NewId”,并为它们分配第一种颜色,其他条目与下一个类似的“NewId”和第二种颜色等等,直到前10个相同“NewId”的项目得到分配各自的颜色。
例如: - 分组前
NewId Title Description
101 title1 des1
102 title2 des2
103 title3 des3
101 title4 des4
102 title5 des5
103 title6 des6
分组后,
NewId Title Description
101 title1 des1 ------> color1
101 title4 des4 ------> color1
102 title2 des2 ------> color2
102 title5 des5 ------> color2
103 title3 des3 ------> color3
103 title6 des6 ------> color3
我还做了什么: -
public class MyList {
private ArrayList<HashMap<String, String>> list = new ArrayList<>();
public boolean add(HashMap<String, String> map) {
return list.add(map);
}
public void setColor(String newId, String color) {
for (HashMap<String, String> m : list)
if (m.containsKey(newId))
m.put("color", color);
}
public String getGroupKey(String key, int i) {
ArrayList<String> uniqeList = getUniqKeyList(key);
Collections.sort(uniqeList);
return uniqeList.get(i);
}
public ArrayList<String> getUniqKeyList(String key){
ArrayList<String> l = new ArrayList<>();
for (HashMap<String, String> m : list)
if(!l.contains(m.get(key)))
l.add(m.get(key));
return l;
}
}
public static void main(String[] args) throws Exception {
MyList myList = new MyList();
HashMap<String,String> map = new HashMap<String,String>();
map.put("NewId", newId);
map.put("Title", title);
map.put("Description", description);
myList.add(map);
String[] colors = new String[]{"#1F1A17", "#62934D","#B694D1"};
int i=0;
while (true) {
if(i == colors.length)
break;
String s = myList.getGroupKey("NewId", i);
if(s == null)
break;
else
myList.setColor(s, colors[i++]);
}
}
itemsAdapter = new LazyAdapter(myContext, myList);
但是我收到了一个错误: -
`E/AndroidRuntime(10276): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1`
我该如何解决这个问题?
答案 0 :(得分:1)
如果您的问题与异常有关,那么您特定情况下的问题在:
1此方法始终只返回1个元素的数组列表:
public ArrayList<String> getUniqKeyList(String key) {
ArrayList<String> l = new ArrayList<>();
for (HashMap<String, String> m : list)
if(!l.contains(m.get(key)))
l.add(m.get(key));
return l;
}
2个颜色数组的长度为3。
3第一次迭代,而i = 0就可以了。
4次迭代(i = 1&lt; color.length = 3)在以下问题中存在问题:
public String getGroupKey(String key, int i) {
ArrayList<String> uniqeList = getUniqKeyList(key);
Collections.sort(uniqeList);
return uniqeList.get(i);
}
uniqeList.get(i)= uniqeList.get(1),但uniqeList的长度为1(从0开始计算)。
答案 1 :(得分:0)
尝试检查索引是否在列表中可用,因为列表只有一个值,但您尝试获取多个
public String getGroupKey(String key, int i)
{
ArrayList<String> uniqeList = getUniqKeyList(key);
Collections.sort(uniqeList);
if (uniqeList.size() > i) // check here whether the list size is greater than index
{
return uniqeList.get(i);
}
else // else it returns an empty you can change by your concern
{
return "";
}
}
而不是
public String getGroupKey(String key, int i) {
ArrayList<String> uniqeList = getUniqKeyList(key);
Collections.sort(uniqeList);
return uniqeList.get(i);
}
答案 2 :(得分:0)
public ArrayList<String> getUniqKeyList(String key){
ArrayList<String> l = new ArrayList<>();
for (HashMap<String, String> m : list)
if(!l.contains(m.get(key)))
l.add(m.get(key));
return l;
}
您是否有条件将条目添加到列表中
public String getGroupKey(String key, int i) {
ArrayList<String> uniqeList = getUniqKeyList(key);
Collections.sort(uniqeList);
return uniqeList.get(i);
}
由于您未在此处检查相同的条件,因此无法确定您的数据是否一致 - key
和i
之间的相关性将丢失。