Android: How to update ListView periodically?

I have never worked so straight. Sorry if I'm a little vague. I will try to talk about what I'm trying to do. I am trying to create a list that captures its data from webservice. When I initialize listview, I want to periodically check the web server and update the contents of the list. For this, I am doing something like this:

public class SampleAutoUpdateList extends Activity { //Autoupdate handler private Handler handler = new Handler(); private Runnable updater = new Runnable() { public void run() { /* * Update the list */ try { Log.i("UPDATE", "Handler called"); searchAdapter = getFeed(URL); searchAdapter.notifyDataSetChanged(); handler.postDelayed(this, Configuration.REFRESH_INTERVAL); } catch(Exception e) { Log.e("UPDATE ERROR", e.getMessage()); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linearmode); this.context = this; searchAdapter = getFeed(URL); LinearLayout l2 = (LinearLayout) findViewById(R.id.secondaryLayout); ListView list = new ListView(context); l2.addView(list); // display UI UpdateDisplay(list); updater.run(); } private SearchAdapter getFeed(String URL) { try { SearchHandler handler = new SearchHandler(); URL url = new URL(URL); String data = convertStreamToString(url.openStream()); data = data.substring(data.indexOf('['), data.length()-1); handler.parseJSON(data); return handler.getFeed(); } catch (Exception ee) { // if we have a problem, simply return null Log.e("getFeed", ee.getMessage()); return null; } } private void UpdateDisplay(View searchView) { // TODO Auto-generated method stub // TODO Auto-generated method stub searchList = (ListView) searchView; myProgressDialog = ProgressDialog.show(this, "Please wait...", "Loading search....", true); new Thread() { public void run() { try{ Thread.sleep(2000); } catch (Exception e) { } runOnUiThread(new Runnable() { @Override public void run() { if (searchAdapter == null) { Log.e("ERROR", "No Feed Available"); return; } searchAdapter.setContext(context); searchList.setAdapter(searchAdapter); searchList.setSelection(0); } }); // Dismiss the Dialog myProgressDialog.dismiss(); } }.start(); } } 

And the SearchHandler class is simple:

 public class SearchHandler extends DefaultHandler { SearchAdapter _adapter; SearchItem _item; public SearchHandler() { } public SearchAdapter getFeed() { return _adapter; } public void parseJSON(String data) { // TODO Auto-generated method stub _adapter = new SearchAdapter(); JSONArray parseArray; try { parseArray = new JSONArray(data); for (int i=0; i < parseArray.length(); i++) { SearchItem item = new SearchItem(); JSONObject jsonUser = parseArray.getJSONObject(i); item.set_from(jsonUser.getString ("from")); item.set_msg(jsonUser.getString("msg")); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

No matter what I do, the handler gets called and new items are retrieved, but the list is never updated. Any ideas on what might be wrong?

+4
source share
1 answer

Well, it’s a little difficult to follow your code, since you only have a fragment of it and a few really relevant bits. For example, based on your available code, your list should be forever empty, since you never associate a searchAdapter with a ListView ... at least in the code you showed.

However, the following lines look especially strange:

  searchAdapter = getFeed(URL); searchAdapter.notifyDataSetChanged(); 

I am going to assume that getFeed() (not shown) creates a new ListAdapter . If getFeed() creates a new ListAdapter , there is no need to call notifyDataSetChanged() on it, since its data set has not changed - it is completely new. Moreover, if you do not associate this new ListAdapter with your ListView , the new ListAdapter will have no effect.

If I creep up the wrong tree, consider adding lines to your sample that show the implementation of getFeed() and where you use the searchAdapter .

+4
source

Source: https://habr.com/ru/post/1300249/


All Articles