Hello, here comes this from the server, nothing else:

{ "Ключ": Значение, "Ключ2": Значение, "Ключ3": Значение } 

I want to shove both the key and the value in the ArrayList<HashMap<String, Object>> loop, for example:

  hm = new HashMap<>(); hm.put(NAME, "КЛЮЧ"); hm.put(VALUE, "ЗНАЧЕНИЕ"); list.add(hm) 

And then through SimpleAdapter output to the ListView something like this:

 adapter = new SimpleAdapter(this, CountersList, R.layout.list_downtime, new String[]{NAME, VALUE}, new int[]{R.id.date, R.id.downtime}); listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter); 

How to do it?

  • that does not work? - Android Android
  • I want to shove both the key and the value in ArrayList <HashMap <String, Object >> cycle - it doesn't work - expertgames

1 answer 1

To begin with, a couple of principles of ListView , namely, why does he need List<Map<String, Object>> . ListView displays a collection of items. Each item contains a set of some data. That is, on the screen you will see a list of items, and each item has its own list of data. Thus, one element is a key-value map , where the keys are responsible for storing data, and the values ​​are obtained and output . A list of such elements containing a map with several keys and values ​​will provide a list output to the screen, so we put all our maps in the List . Thus, your task "to bring out both the key and the value in the list element" somewhat violates the principles of work, and I see its correct solution differently (I will write at the end). But she has the right to exist for "educational purposes . "

I handled JSON with my hands, without third-party libraries like GSON, Jackson, etc.
Did it like this:

 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; StringBuilder s; //строка, содержащая весь JSON dataEntries = new ArrayList<Map<String, String>>(); //список мапов для передачи адаптеру ... JSONArray data = new JSONArray(s.toString()); for(int i=0; i<data.length(); i++) { //обходим JSONArray (коллекция) JSONObject dataEntry = data.getJSONObject(i); //каждый JSON-объект из коллекции Iterator<String> key = dataEntry.keys(); //коллекция ключей JSON-объекта Map<String, String> entry = new HashMap<>(); //новый map, в который положить прочитанные ключ/значение while(key.hasNext()){ //обходим все ключи JSON-объекта String nextKey = key.next(); entry.put(nextKey, dataEntry.getString(nextKey)); //и кладем их в map } dataEntries.add(entry); //добавляем map в список мапов для адаптера } 

As a result, one element of the ListView list displays one JSON object: only the values ​​of all its keys (see the last image). I didn’t need the names of the JSON keys in the ListView . You want to receive a conclusion in an element of the list namely "key" and "value". Therefore, in your case, adding a JSONArray collection to the map will be little meaningful: you cannot separate the fields of one object from the fields of another. So you can only display one JSON object in this way, and your entire ListView will display only one JSON object, each ListView element will correspond to one field “key - value” of the object, and will contain 2 lines: key and value. Attention, actually the answer to your question.

 JSONObject dataEntry = new JSONObject(s.toString()); //строка JSON содержит только один объект Iterator<String> key = dataEntry.keys(); while(key.hasNext()){ //обходим все ключи JSON-объекта String nextKey = key.next(); entry.put("KEY", nextKey) //кладем ключ entry.put("VALUE", dataEntry.getString(nextKey)); //кладем значение dataEntries.add(entry); //добавляем в список ключ и значение } 

As a result, you will get this: enter image description here

Now a few words about how I consider it correct to implement the output of both the key and the value. You need to create xml markup of one item.xml screen element of the item.xml type: enter image description here Then write your adapter (this is well written here ) and give it a well-formed map , like in my first code example. And inside this adapter, display the keys in the left margins, and the corresponding values ​​in the right margins. Correct output of keys and values: enter image description here

Hopefully explained why it’s “wrong to want what you want” :)

View of the list containing only values ​​(you will have a useless list that does not separate the names of the keys from their values): Screen of my list view

  • Thank you very much, I understood everything, everything turned out, unfortunately, the data comes to me and I cannot do anything about it .... - expertgames
  • one
    Only one object arrives? Then you don't need a ListView to display it. If several objects come in (it doesn’t matter at one time or “individually”), then you need to do it right. The question is not how the data comes. The question is what exactly you will do with them and how will you display them on the screen. So you can do everything :) You are a programmer, and you have to produce a quality product. The potential user does not care about the implementation details, along with the fact that something supposedly does not depend on you. - Pavel Krizhanovskiy