Faced such a problem in your application:

There is a JSON file located on a remote server, in its application added a class inherited from AsyncTask , which parses the data from JSON and displays the data on the device screen using ListView . In the onCreate method of the onCreate class, MainActivity create an object of the class previously written, thereby performing the task (parse the JSON data and display it on the screen).

Also added a button to update the information. In the event handler for this button, I clear the setAdapter my ListView and create a new class object that parses the data. It was assumed that there would be just an update of the information in the list, but it turned out that the newly updated data are written simply at the end of the list.

Tell me, how can I update the necessary information?

Adapter code to add entries to the ListView:

 ListAdapter adapter = new SimpleAdapter( MainActivity.this, itemsList, R.layout.list_item, new String[]{ "currentPrice", "basePrice", "route", "bidsCount"}, new int[]{R.id.currentPrice, R.id.basePrice, R.id.route, R.id.bidsCount} ); listView.setAdapter(adapter); 

Event handler for Refresh button:

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listView.setAdapter(null); new GetInfo().execute(); } }); 

Part of the GetInfo class, namely the doInBackground(Void... arg0) method doInBackground(Void... arg0) , where parsing and adding data to the HashMap takes place:

 for (int i = 0; i < items.length(); i++) { JSONObject item = items.getJSONObject(i); JSONObject data = item.getJSONObject("data"); JSONObject bid = item.getJSONObject("bid"); JSONObject bidData = bid.getJSONObject("data"); String currentPrice = bidData.getString("currentPrice"); String basePrice = bidData.getString("basePrice"); String bidsCount = data.getString("bidsCount"); String route = data.getString("route"); HashMap<String, String> itemHashMap = new HashMap<>(); itemHashMap.put("currentPrice", getString(R.string.current_price) + " " + currentPrice); itemHashMap.put("basePrice", getString(R.string.base_price) + " " + basePrice); itemHashMap.put("route", getString(R.string.route) + " " + route); itemHashMap.put("bidsCount", getString(R.string.bids_count) + " " + bidsCount); itemsList.add(itemHashMap); } 
  • Display the adapter code and method code that clears the ListView . - post_zeew Nov.
  • @post_zeew, corrected - osip_000
  • Then you need the GetInfo code. - post_zeew Nov.
  • @post_zeew, the class turned out to be more than 100 lines, so I see no reason to give the code for the entire class. Added the main where parsing and adding of information in HashMap occurs - osip_000
  • itemsList is an object of what class? - post_zeew

1 answer 1

In the event handler for this button, I clear the setAdapter my ListView

In line:

 listView.setAdapter(null); 

You do not clear the data, you simply reset the adapter. The data was in the itemsList , so it remains there.

Before adding new data you need to clear the itemsList collection:

 itemsList.clear(); itemsList.add(itemHashMap); 

A reset adapter is not necessary.

  • Thank you very much, everything worked :) There was just a itemsList.clear(); - osip_000
  • But could you explain why you should not reset the adapter? After all, so ListView disappears for a couple of moments and I get the feeling that the information has really been updated. Yes, I have Toast, which says that there is an update, but still ... - osip_000
  • @AndreiO. And why reset the adapter, and then re-create a new one? If you want the list to be hidden during an update, simply hide the ListView using the listView.setVisibility(ListView.INVISIBLE); method listView.setVisibility(ListView.INVISIBLE); . - post_zeew Nov.