I want to retrieve a Dictionary> data from a server into an arraylist to populate it in a listview in android. I have done the connection to the server and what the response is in format:{"Key":["value1","value2"],"key":["value1",value2"]}.
Thanks for any help you can provide.
Try using AbstractMap
And store that in an arrayist as follows...
List<Map.Entry<String, Object>> myArrayList = new ArrayList<Map.Entry<String, Object>>();
So now you have an arraylist of key value mappings which you can populate and use in your list view
Related
I'm trying to implement an expandable listview with data coming from a database. I've already tried having the data added within the codes and it works so I'm now trying to have dynamic data from the database populate the listview.
The data is grouped in two, the main category and the member. For example if the main category is fruit, it's members may include mango, avocado, apples, etc. If animals, it may have horses, eagle, shark. The data always comes in pairs such as Animal, Horses; Animal, Eagle; Fruit, Apples. The expandable listview should appear as:
Animal
-- Horses
-- Eagle
Fruit
-- Apples
My code below can already determine the main group so the headers already display the main groupings of Fruit and Animal. By using
group_member.put(family_name, member_name);
I linked the member to the main category. My problem now is how to code for the iteration to group the members to the main category. So far I've already tried using iterator based on the sample codes given here in stackoverflow and from other sites however only the last elements of each group is display. I've also tried using the for-each loop bur still no success.
String json = jsonParser.makeHttpRequest(URL_COMPONENTS, "POST", params);
JSONArray components = null;
ArrayList<HashMap<String, String>> componentList;
List<String> main_group = new ArrayList<String>();
HashMap<String, String> group_member = new HashMap<String, String>();
try {
components = new JSONArray(json);
if (components != null) {
for (int i = 0; i < components.length(); i++) {
JSONObject c = components.getJSONObject(i);
String family_name = c.getString(TAG_FAMILY_NAME);
String member_name = c.getString(TAG_MEMBER_NAME);
main_group.add(family_name);
group_member.put(family_name, member_name);
if (!listDataHeader.contains(main_group))
listDataHeader.add(main_group);
componentList.add(group_member);
}
}
}
Please help. Thanks in advance!
If you're using HashMap you can't have a key more than once in your list. Each key must be unique.
This could be a solution:
HashMap<String, ArrayList<String>> group_member = new HashMap<String, ArrayList<String>>();
... using the Hashmap as a directory and the key is returning an Arraylist of the family members.
I want to pass Array list from one activity to another activity. I am trying like this:
I am passing array list source activity to destination activity. But the problem is I am getting only the last item at destination activity.
My code is
source.class
HashMap<String,String> hm = new HashMap<String, String>();
ArrayList<HashMap<String,String>> arl = new ArrayList<HashMap<String,String>>();
hm.put(KEY_NAME,u);//am adding these values through loop
arl.add(hm);//adding Hash Map to Array List
Intent intent = new Intent(MainActivity.this, SinglePlaceActivity.class);
intent.putExtra("arraylist", arl
startActivityForResult(intent, 500);
System.out.println("uuuuu"+arl);//upto now working good and display perfectly all array list
destination.class
ArrayList<HashMap<String, String>> arl = ArrayList<Hash
Map<String,String>>)getIntent().getSerializableExtra("arraylist");
System.out.println(arl);//am getting what i add last item in the Arrylist at source class
Iterator itr = arl.iterator();
while(itr.hasNext())
{
System.out.println(itr.hasNext);//am getting single last item multiple times.what i add last item in the Arrylist at source class
I want to display source class array list into destination class.
Try using:
Bundle.putSerializable() and Bundle.getSerializable().
Also, in:
System.out.println(itr.hasNext);
shouldn't it be its.next()?
Take also into account that using Seralizable object in Android IPC adds a significant overheard with respect to using Parcelable objects.
Im using hashmap and arraylist...
How to sort the arraylist ? eg) In hashmap values are in order like one,two,three,four,five
but i stored these values in arraylist the order changed like three,one,five,two,four
In my code groupList,gnamelist and newList are all arraylist...
In print sts PLACES are in correct order but while print on NEWLIST PLACES the order changed
How to sort this in order?
My code
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PLACE, gname);
map.put(TAG_HOTEL,lname);
// adding HashList to ArrayList
groupList.add(map);
gnamelist.add(gname);
System.out.println("PLACES" + gnamelist);
List<String> newList = new ArrayList<String>(new LinkedHashSet<String>(gnamelist));
Collections.sort(newList,Collections.reverseOrder());
System.out.println("NEWLIST PLACES" + newList);
HashSet will store elements in an unordered fashion, and is likely the culprit of your element reordering.
List<String> newList = new ArrayList<String>(new HashSet<String>(gnamelist));
Also consider using LinkedHashMap/LinkedHashSet, which preserves the ordering of elements added to it.
Alternatively try the following:
gnamelist.add(gname);
System.out.println("PLACES" + gnamelist);
List<String> newList = new ArrayList<String>();
newList.addAll(gnamelist);
Try
Collections.sort(newList);
Edit:
If you want to sort in reverse use this
Collections.sort(newList,Collections.reverseOrder());
Important:
if you want to preserve insertion order, you need to use TreeSet instead of HashSet as HashSet doesn't preserve insertion order
Hello I am parsing some json on android and I have been able to put the json parsed data into a listview. However on my listview, I also have some icons that I would like to set the status according to the parsed data ( I have some keys that define their status, 1 for enabled and 0 for disabled). I can't seem to put this on the HashMap, anyone has any tip/information?
Thanks
HashMap<String,Boolean> map = new HashMap<String,Boolean>();
Collection c = map.values();
Iterator itr = c.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
im wondering, how can I sort my arraylist:
its look like this:
ArrayList<HashMap<String, ?>> list = new ArrayList<HashMap<String, ?>>();
temp.put("Position", count);
list.add(temp);
I would sort by Position key.
please try it.
Comparator comparator = Collections.reverseOrder();
Collections.sort(list,comparator);
And also check it.
Sort a Map<Key, Value> by values (Java)
If what you want is to sort the map entries by their keys, you can use a SortedMap (e.g. TreeMap):
List<SortedMap<String, ?>> list = new ArrayList<SortedMap<String, ?>>();