How to display a LinkedHashMap < string, LinkedHashMap <string, string>> on Android - android

I have a LinkedHashMap of contacts. For each contact I save inside the LinkedHashMap , a new LinkedHashMap with a variable amount of results of an Action. For example,
Contact 1(Key)
Action1(Key) - 2/10(Value)
Action2 - 3/10
.....
Action10 - 3/5
Contact 2
Action1 - 2/10
Action2 - 3/10
Action3 - 4/3
How I am going to put them in the following order like:
Action1 Action2 Action3 ..... Action10
Contact1 result
Contact2
To display them in gridview or tablelayout or webview?
I am trying with following to access elements but I get all of them in the arraylist
ArrayList Valuestrings = new ArrayList();
ArrayList KValuestrings = new ArrayList();
for (Map.Entry<String, LinkedHashMap<String, String>> entry : LnkHshPlayerData.entrySet())
{
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet())
{
KValuestrings.add(entry2.getKey());
Valuestrings.add(entry2.getValue());
}
}

i'm not sure it's what you ask for but if you do:
ArrayList strings = new ArrayList();
HashMap<String, HashMap<String, String>> selects = new HashMap<String, HashMap<String, String>>();
for (Map.Entry<String, HashMap<String, String>> entry : selects.entrySet()) {
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet()) {
strings.add(entry2.getValue());
}
}
gridViewAdapter.addAll(strings);

Related

How to find the dimensions of a List<HashMap<String, String>>

I want to loop through the a List of List>
I need to find the ceiling of the list so I can use it in the For statement
This is my code and I dont know what to use in the last line
**List<HashMap<String, String>> listMsgs = null;
listMsgs = msgsXmlParser.detparse(reader);
for(int i=0; i< listMsgs.**
Normally you would use listMsgs.size() or you can do this:
for (Map<String, String> map : listMsgs) {
// work with map here
}
listMsgs.size() will give you a number of HashMaps in the list
listMsgs.get(i).size() will give you the size of HashMap at index i
List<HashMap<String, String>> listMsgs = null;
listMsgs.size();
//size of collection
for(HashMap<String, String> lMap : listMsgs)
//iterating through collection
{
lMap.size();
//size of map
for(String lKey : lMap.keySet())
//iterating through map keyset
{
lMap.get(lKey);
//key value
}
}

Listview repeating information

I am trying to do a listview with 2 database collumn. I succeded but my problem is that only the first information of the database appears in the listview. If i have x data in the database it will show the x rows in the listview with only the first entry.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);
if (cursor.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
data.clear();
do {
data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
data.add(cursor.getString(cursor.getColumnIndex("nome")));
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
mylist.add(map);
} while (cursor.moveToNext());
SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
lista.setAdapter(resul);
I see two problems in your code:
You don't clear data (Like said in a previous answer) so you always get the first two values
You always use the same map. Since map is an object you will always modify the same and your list will be fill will one map.
To fix this two points try this:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);
if (cursor.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
do {
HashMap<String, String> map = new HashMap<String, String>();
data.clear();
data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
data.add(cursor.getString(cursor.getColumnIndex("nome")));
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
mylist.add(map);
} while (cursor.moveToNext());
SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
lista.setAdapter(resul);
look at this,
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
You always put the same key in the map, always "id, nome and hora" you need put diferent key.
ps: Why you create a List of Map? You don't need to this.
You need to clear data each time through the loop. You need to move your call to data.clear() to just after the do { loop begins. What you're doing at the moment will mean that after the first item is retrieved from the cursor, data will contain 2 items. After the second time it will contain 4, then 6, etc. You then always read the same two items. If you clear data it should fix your problem.

Using statements for common object types

In Xamarin, I am trying this tutorial: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html
Here is the code:
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.Put(key, name);
return planet;
}
May I please have some help with the correct using statements for the following types:
Map
ArrayList
HashMap
Thanks in advance
Map and ArrayMap are in java.util. ArrayList is in System.Collections.

Issue with Hashmap. Can not return All values when it has same key. How to get all values?

private HashMap<String, String> mHashMap = new HashMap<String, String>();
mHashMap.put("a", "ajay");
mHashMap.put("h", "hiren");
mHashMap.put("d", "darshan");
mHashMap.put("a", "anand");
mHashMap.put("h", "harsah");
for (String key: mHashMap.keySet()) {
Log.e(key,"" + mHashMap.get(key));
}
My Result:
d - darsha
h - harshad
a - anand
I want all values from HashMap?
If you have any idea related to it, then help me.
HashMap will override the value if a key for that value exists already. If you need to have more values for a key, you should use a Collection as value fro your HashMap:
private HashMap<String, Vector<String>> mHashMap = new HashMap<String, Vector<String>>();
Vector<String> tmp = new Vector<String>();
tmp.add("ajay");
tmp.add("anand");
mHashMap.put("a", tmp);
tmp = new Vector<String>();
tmp.add("hiren");
mHashMap.put("h", tmp);
and so on..

adding data to a listview from string array

I am trying to create a 4 column listview using arrayAdapter, and add values manually in the populateList()
private void populateList()
{
list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put(FIRST_COLUMN,"Diaries");
temp1.put(SECOND_COLUMN, "Products");
temp1.put(THIRD_COLUMN, "Rs. 400");
temp1.put(FOURTH_COLUMN, "ggg Unit");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put(FIRST_COLUMN,"Note Books");
temp2.put(SECOND_COLUMN, "Products");
temp2.put(THIRD_COLUMN, "Rs. 600");
temp2.put(FOURTH_COLUMN, "hhh Unit");
list.add(temp2);
}
My problem is i want to add list items dynamically from a two dimensional array
How it is possible??? the item count have the same size of the string array??
ie; insted of
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put(FIRST_COLUMN,"Diaries");
temp1.put(SECOND_COLUMN, "Products");
temp1.put(THIRD_COLUMN, "Rs. 400");
temp1.put(FOURTH_COLUMN, "ggg Unit");
i want to display
temp1.put(FIRST_COLUMN,"myArry[i][j]");
temp1.put(SECOND_COLUMN, "myArry[i][j]");
temp1.put(THIRD_COLUMN, "myArry[i][j]");
temp1.put(FOURTH_COLUMN, "myArry[i][j]");
in a for loop
also my doubut is how to create hashmap dynamically
I didn't quite understand what you want exactly, but may you can try this:-
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp = null;
for(int i=0;i<2;i++){
temp = new HashMap<String,String>();
for(int j=0;j<4;j++){
temp.put(String.valueOf(j), myArr[i][j]);
}
list.add(temp);
}
Note:- Instead of FIRST_COLUMN, SECOND_COLUMN, you'll have to use 1,2,3.. as your keys for HashMap.
try some thing like this.
HashMap<String,String> temp1 = null;
for (int i = 0; i < myArry.length; i++) {
temp1 = new HashMap<String, String>();
temp1.put(FIRST_COLUMN, myArry[i][0]);
temp1.put(SECOND_COLUMN, myArry[i][1]);
temp1.put(THIRD_COLUMN, myArry[i][2]);
temp1.put(FOURTH_COLUMN, myArry[i][3]);
list.add(temp1);
}
In your code I no where see a list view creation. You need to create a list view in your xml and populate the data using an adapter

Categories

Resources