How to iterate through a hashmap to get every fourth key, value pair?
up vote
0
down vote
favorite
I have a hashmap like this;
key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on
Now I want to put this in a ListView which contains four rows, and in row1, I want:
key value1
key1 value5
key2 value9
key3 value13 and so on till end
And in row2, I want:
key value2
key1 value6
key2 value10
key3 value14 and so on till end
And then the same in rows 3 and 4. There are always only 4 rows.
I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.
String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);
for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();
listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}
//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);
Thanks.
java android loops listview hashmap
add a comment |
up vote
0
down vote
favorite
I have a hashmap like this;
key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on
Now I want to put this in a ListView which contains four rows, and in row1, I want:
key value1
key1 value5
key2 value9
key3 value13 and so on till end
And in row2, I want:
key value2
key1 value6
key2 value10
key3 value14 and so on till end
And then the same in rows 3 and 4. There are always only 4 rows.
I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.
String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);
for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();
listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}
//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);
Thanks.
java android loops listview hashmap
1
Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35
Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50
How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22
@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12
@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a hashmap like this;
key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on
Now I want to put this in a ListView which contains four rows, and in row1, I want:
key value1
key1 value5
key2 value9
key3 value13 and so on till end
And in row2, I want:
key value2
key1 value6
key2 value10
key3 value14 and so on till end
And then the same in rows 3 and 4. There are always only 4 rows.
I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.
String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);
for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();
listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}
//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);
Thanks.
java android loops listview hashmap
I have a hashmap like this;
key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on
Now I want to put this in a ListView which contains four rows, and in row1, I want:
key value1
key1 value5
key2 value9
key3 value13 and so on till end
And in row2, I want:
key value2
key1 value6
key2 value10
key3 value14 and so on till end
And then the same in rows 3 and 4. There are always only 4 rows.
I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.
String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);
for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();
listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}
//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);
Thanks.
java android loops listview hashmap
java android loops listview hashmap
edited Nov 6 at 4:17
asked Nov 5 at 2:13
Zac
3001421
3001421
1
Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35
Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50
How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22
@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12
@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15
add a comment |
1
Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35
Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50
How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22
@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12
@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15
1
1
Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35
Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35
Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50
Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50
How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22
How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22
@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12
@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12
@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15
@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.
Multimap myList = ArrayListMultimap.create();
myList.put("key","value1");
myList.put("key","value2");
myList.put("key","value3");
myList.put("key","value4");
myList.put("key1","value5");
myList.put("key1","value6");
myList.put("key1","value7");
myList.put("key1","value8");
myList.put("key2","value9");
myList.put("key2","value10");
myList.put("key2","value11");
myList.put("key2","value12");
myList.put("key3","value13");
myList.put("key3","value14");
myList.put("key3","value15");
myList.put("key3","value16");
Then get all keys from list
Set myKeySet = myList.keySet();
Object keys = myKeySet.toArray();
Now you have set of all key. then
HashMap<String, String> listHashMap1 = new HashMap<>();
HashMap<String, String> listHashMap2 = new HashMap<>();
HashMap<String, String> listHashMap3 = new HashMap<>();
HashMap<String, String> listHashMap4 = new HashMap<>();
Now add the value to every list
for(int i=0;i<4;i++){
List<String> keyval = (List)myList.get((String)keys[i]);
listHashMap1.put((String)keys[i],keyval.get(0));
listHashMap2.put((String)keys[i],keyval.get(1));
listHashMap3.put((String)keys[i],keyval.get(2));
listHashMap4.put((String)keys[i],keyval.get(3));
}
Now you have set of four row list. This Multimap is using google guava.
So you need to implement guava in your gradle as below:
implementation 'com.google.guava:guava:26.0-jre'
add a comment |
up vote
0
down vote
From the code you supplied it seems that the source hashMap
is of type Map<String, String>
.
You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:
Map<String, String> one = new HashMap<>();
Map<String, String> two = new HashMap<>();
Map<String, String> three = new HashMap<>();
Map<String, String> four = new HashMap<>();
hashMap.entrySet().forEach(entry -> {
String values = entry.getValue();
one.put(entry.getKey(), values[0]);
two.put(entry.getKey(), values[1]);
three.put(entry.getKey(), values[2]);
four.put(entry.getKey(), values[3]);
});
Note: assuming every key always has exactly four values.
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
add a comment |
up vote
-1
down vote
I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:
Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.
ArrayList<String> row1 = new ArrayList<>();
ArrayList<String> row2 = new ArrayList<>();
ArrayList<String> row3 = new ArrayList<>();
ArrayList<String> row4 = new ArrayList<>();
for(int i = 1; i<= hashMap.size(); i++){
if(i%4 == 1){
int keynum = i - 1;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
//Assuming that the values are integers,
row1.add(st +" " + hashMap.get(st));
}else if(i%4 == 2){
int keynum = i - 2;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row2.add(st +" " + hashMap.get(st));
}else if(i%4 == 3){
int keynum = i - 3;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row3.add(st +" " + hashMap.get(st));
}else if(i%4 == 0){
int keynum = i;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row4.add(st +" " + hashMap.get(st));
}
}
Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.
I hope this helps.
New contributor
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.
Multimap myList = ArrayListMultimap.create();
myList.put("key","value1");
myList.put("key","value2");
myList.put("key","value3");
myList.put("key","value4");
myList.put("key1","value5");
myList.put("key1","value6");
myList.put("key1","value7");
myList.put("key1","value8");
myList.put("key2","value9");
myList.put("key2","value10");
myList.put("key2","value11");
myList.put("key2","value12");
myList.put("key3","value13");
myList.put("key3","value14");
myList.put("key3","value15");
myList.put("key3","value16");
Then get all keys from list
Set myKeySet = myList.keySet();
Object keys = myKeySet.toArray();
Now you have set of all key. then
HashMap<String, String> listHashMap1 = new HashMap<>();
HashMap<String, String> listHashMap2 = new HashMap<>();
HashMap<String, String> listHashMap3 = new HashMap<>();
HashMap<String, String> listHashMap4 = new HashMap<>();
Now add the value to every list
for(int i=0;i<4;i++){
List<String> keyval = (List)myList.get((String)keys[i]);
listHashMap1.put((String)keys[i],keyval.get(0));
listHashMap2.put((String)keys[i],keyval.get(1));
listHashMap3.put((String)keys[i],keyval.get(2));
listHashMap4.put((String)keys[i],keyval.get(3));
}
Now you have set of four row list. This Multimap is using google guava.
So you need to implement guava in your gradle as below:
implementation 'com.google.guava:guava:26.0-jre'
add a comment |
up vote
0
down vote
Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.
Multimap myList = ArrayListMultimap.create();
myList.put("key","value1");
myList.put("key","value2");
myList.put("key","value3");
myList.put("key","value4");
myList.put("key1","value5");
myList.put("key1","value6");
myList.put("key1","value7");
myList.put("key1","value8");
myList.put("key2","value9");
myList.put("key2","value10");
myList.put("key2","value11");
myList.put("key2","value12");
myList.put("key3","value13");
myList.put("key3","value14");
myList.put("key3","value15");
myList.put("key3","value16");
Then get all keys from list
Set myKeySet = myList.keySet();
Object keys = myKeySet.toArray();
Now you have set of all key. then
HashMap<String, String> listHashMap1 = new HashMap<>();
HashMap<String, String> listHashMap2 = new HashMap<>();
HashMap<String, String> listHashMap3 = new HashMap<>();
HashMap<String, String> listHashMap4 = new HashMap<>();
Now add the value to every list
for(int i=0;i<4;i++){
List<String> keyval = (List)myList.get((String)keys[i]);
listHashMap1.put((String)keys[i],keyval.get(0));
listHashMap2.put((String)keys[i],keyval.get(1));
listHashMap3.put((String)keys[i],keyval.get(2));
listHashMap4.put((String)keys[i],keyval.get(3));
}
Now you have set of four row list. This Multimap is using google guava.
So you need to implement guava in your gradle as below:
implementation 'com.google.guava:guava:26.0-jre'
add a comment |
up vote
0
down vote
up vote
0
down vote
Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.
Multimap myList = ArrayListMultimap.create();
myList.put("key","value1");
myList.put("key","value2");
myList.put("key","value3");
myList.put("key","value4");
myList.put("key1","value5");
myList.put("key1","value6");
myList.put("key1","value7");
myList.put("key1","value8");
myList.put("key2","value9");
myList.put("key2","value10");
myList.put("key2","value11");
myList.put("key2","value12");
myList.put("key3","value13");
myList.put("key3","value14");
myList.put("key3","value15");
myList.put("key3","value16");
Then get all keys from list
Set myKeySet = myList.keySet();
Object keys = myKeySet.toArray();
Now you have set of all key. then
HashMap<String, String> listHashMap1 = new HashMap<>();
HashMap<String, String> listHashMap2 = new HashMap<>();
HashMap<String, String> listHashMap3 = new HashMap<>();
HashMap<String, String> listHashMap4 = new HashMap<>();
Now add the value to every list
for(int i=0;i<4;i++){
List<String> keyval = (List)myList.get((String)keys[i]);
listHashMap1.put((String)keys[i],keyval.get(0));
listHashMap2.put((String)keys[i],keyval.get(1));
listHashMap3.put((String)keys[i],keyval.get(2));
listHashMap4.put((String)keys[i],keyval.get(3));
}
Now you have set of four row list. This Multimap is using google guava.
So you need to implement guava in your gradle as below:
implementation 'com.google.guava:guava:26.0-jre'
Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.
Multimap myList = ArrayListMultimap.create();
myList.put("key","value1");
myList.put("key","value2");
myList.put("key","value3");
myList.put("key","value4");
myList.put("key1","value5");
myList.put("key1","value6");
myList.put("key1","value7");
myList.put("key1","value8");
myList.put("key2","value9");
myList.put("key2","value10");
myList.put("key2","value11");
myList.put("key2","value12");
myList.put("key3","value13");
myList.put("key3","value14");
myList.put("key3","value15");
myList.put("key3","value16");
Then get all keys from list
Set myKeySet = myList.keySet();
Object keys = myKeySet.toArray();
Now you have set of all key. then
HashMap<String, String> listHashMap1 = new HashMap<>();
HashMap<String, String> listHashMap2 = new HashMap<>();
HashMap<String, String> listHashMap3 = new HashMap<>();
HashMap<String, String> listHashMap4 = new HashMap<>();
Now add the value to every list
for(int i=0;i<4;i++){
List<String> keyval = (List)myList.get((String)keys[i]);
listHashMap1.put((String)keys[i],keyval.get(0));
listHashMap2.put((String)keys[i],keyval.get(1));
listHashMap3.put((String)keys[i],keyval.get(2));
listHashMap4.put((String)keys[i],keyval.get(3));
}
Now you have set of four row list. This Multimap is using google guava.
So you need to implement guava in your gradle as below:
implementation 'com.google.guava:guava:26.0-jre'
answered Nov 5 at 7:08
kumud kala
415
415
add a comment |
add a comment |
up vote
0
down vote
From the code you supplied it seems that the source hashMap
is of type Map<String, String>
.
You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:
Map<String, String> one = new HashMap<>();
Map<String, String> two = new HashMap<>();
Map<String, String> three = new HashMap<>();
Map<String, String> four = new HashMap<>();
hashMap.entrySet().forEach(entry -> {
String values = entry.getValue();
one.put(entry.getKey(), values[0]);
two.put(entry.getKey(), values[1]);
three.put(entry.getKey(), values[2]);
four.put(entry.getKey(), values[3]);
});
Note: assuming every key always has exactly four values.
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
add a comment |
up vote
0
down vote
From the code you supplied it seems that the source hashMap
is of type Map<String, String>
.
You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:
Map<String, String> one = new HashMap<>();
Map<String, String> two = new HashMap<>();
Map<String, String> three = new HashMap<>();
Map<String, String> four = new HashMap<>();
hashMap.entrySet().forEach(entry -> {
String values = entry.getValue();
one.put(entry.getKey(), values[0]);
two.put(entry.getKey(), values[1]);
three.put(entry.getKey(), values[2]);
four.put(entry.getKey(), values[3]);
});
Note: assuming every key always has exactly four values.
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
add a comment |
up vote
0
down vote
up vote
0
down vote
From the code you supplied it seems that the source hashMap
is of type Map<String, String>
.
You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:
Map<String, String> one = new HashMap<>();
Map<String, String> two = new HashMap<>();
Map<String, String> three = new HashMap<>();
Map<String, String> four = new HashMap<>();
hashMap.entrySet().forEach(entry -> {
String values = entry.getValue();
one.put(entry.getKey(), values[0]);
two.put(entry.getKey(), values[1]);
three.put(entry.getKey(), values[2]);
four.put(entry.getKey(), values[3]);
});
Note: assuming every key always has exactly four values.
From the code you supplied it seems that the source hashMap
is of type Map<String, String>
.
You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:
Map<String, String> one = new HashMap<>();
Map<String, String> two = new HashMap<>();
Map<String, String> three = new HashMap<>();
Map<String, String> four = new HashMap<>();
hashMap.entrySet().forEach(entry -> {
String values = entry.getValue();
one.put(entry.getKey(), values[0]);
two.put(entry.getKey(), values[1]);
three.put(entry.getKey(), values[2]);
four.put(entry.getKey(), values[3]);
});
Note: assuming every key always has exactly four values.
answered Nov 5 at 13:34
Boris van Katwijk
8471821
8471821
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
add a comment |
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
– Zac
Nov 6 at 4:10
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
I have edited the question with how I pass the values to my listView. Please check and let me know.
– Zac
Nov 6 at 4:17
add a comment |
up vote
-1
down vote
I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:
Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.
ArrayList<String> row1 = new ArrayList<>();
ArrayList<String> row2 = new ArrayList<>();
ArrayList<String> row3 = new ArrayList<>();
ArrayList<String> row4 = new ArrayList<>();
for(int i = 1; i<= hashMap.size(); i++){
if(i%4 == 1){
int keynum = i - 1;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
//Assuming that the values are integers,
row1.add(st +" " + hashMap.get(st));
}else if(i%4 == 2){
int keynum = i - 2;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row2.add(st +" " + hashMap.get(st));
}else if(i%4 == 3){
int keynum = i - 3;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row3.add(st +" " + hashMap.get(st));
}else if(i%4 == 0){
int keynum = i;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row4.add(st +" " + hashMap.get(st));
}
}
Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.
I hope this helps.
New contributor
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
add a comment |
up vote
-1
down vote
I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:
Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.
ArrayList<String> row1 = new ArrayList<>();
ArrayList<String> row2 = new ArrayList<>();
ArrayList<String> row3 = new ArrayList<>();
ArrayList<String> row4 = new ArrayList<>();
for(int i = 1; i<= hashMap.size(); i++){
if(i%4 == 1){
int keynum = i - 1;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
//Assuming that the values are integers,
row1.add(st +" " + hashMap.get(st));
}else if(i%4 == 2){
int keynum = i - 2;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row2.add(st +" " + hashMap.get(st));
}else if(i%4 == 3){
int keynum = i - 3;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row3.add(st +" " + hashMap.get(st));
}else if(i%4 == 0){
int keynum = i;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row4.add(st +" " + hashMap.get(st));
}
}
Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.
I hope this helps.
New contributor
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
add a comment |
up vote
-1
down vote
up vote
-1
down vote
I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:
Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.
ArrayList<String> row1 = new ArrayList<>();
ArrayList<String> row2 = new ArrayList<>();
ArrayList<String> row3 = new ArrayList<>();
ArrayList<String> row4 = new ArrayList<>();
for(int i = 1; i<= hashMap.size(); i++){
if(i%4 == 1){
int keynum = i - 1;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
//Assuming that the values are integers,
row1.add(st +" " + hashMap.get(st));
}else if(i%4 == 2){
int keynum = i - 2;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row2.add(st +" " + hashMap.get(st));
}else if(i%4 == 3){
int keynum = i - 3;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row3.add(st +" " + hashMap.get(st));
}else if(i%4 == 0){
int keynum = i;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row4.add(st +" " + hashMap.get(st));
}
}
Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.
I hope this helps.
New contributor
I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:
Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.
ArrayList<String> row1 = new ArrayList<>();
ArrayList<String> row2 = new ArrayList<>();
ArrayList<String> row3 = new ArrayList<>();
ArrayList<String> row4 = new ArrayList<>();
for(int i = 1; i<= hashMap.size(); i++){
if(i%4 == 1){
int keynum = i - 1;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
//Assuming that the values are integers,
row1.add(st +" " + hashMap.get(st));
}else if(i%4 == 2){
int keynum = i - 2;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row2.add(st +" " + hashMap.get(st));
}else if(i%4 == 3){
int keynum = i - 3;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row3.add(st +" " + hashMap.get(st));
}else if(i%4 == 0){
int keynum = i;
keynum = keynum/4;
String st = "key" + Integer.toString(keynum);
row4.add(st +" " + hashMap.get(st));
}
}
Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.
I hope this helps.
New contributor
edited Nov 5 at 13:00
New contributor
answered Nov 5 at 2:33
Ishaan
647
647
New contributor
New contributor
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
add a comment |
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
– Zac
Nov 5 at 3:39
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
– Ishaan
Nov 5 at 13:01
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147453%2fhow-to-iterate-through-a-hashmap-to-get-every-fourth-key-value-pair%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35
Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50
How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22
@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12
@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15