views:

874

answers:

1

I never got this working in a straightforward manner. Sorry if I'm being a little vague. I'll try to elaborate on what I'm trying to do. I am trying to build a listview that grabs its data from a webservice. Once I initialize a listview, I want to keep polling the webserver periodically and update the contents of the listview. 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 the new items are fetched, but the list is never refreshed... Any ideas on what could be going wrong?

+1  A: 

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

That being said, the following lines seem particularly odd:

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

I am going to assume that getFeed() (not shown) creates a new ListAdapter of some sort. If getFeed() is creating a new ListAdapter, there is no need to call notifyDataSetChanged() on it, as its data set hasn't changed -- it's brand new. Moreover, unless you are associating this new ListAdapter to your ListView, the new ListAdapter will have no effect.

If I'm barking up the wrong tree, consider adding lines to your sample showing the implementation of getFeed() and where you are using searchAdapter.

CommonsWare
Sorry again.. I was just thinking that if the posted is huge, no one will bother to look at it. But now I realize that, the part I skipeed is the most important.. Updated with all relevant parts...
Legend
OK, well, then, I think my answer is accurate. Your SearchHandler creates new adapters each time. However, I only see you associating the adapter with the ListView once, in UpdateDisplay(). Hence, the second and subsequent adapters are just created and discarded, near as I can tell.
CommonsWare
Hmm.. For some reason, I never expected this to be a problem. So do I re-associate it with the ListView everytime or do you suggest any other method?
Legend
Well, there are two approaches. First, you could update your existing SearchAdapter in-place, using notifyDataSetChanged() to trigger the ListView update. Since I don't think you have the SearchAdapter code posted, I don't know if that will be easy or difficult. Second, you could call setAdapter() on the ListView every time you build a new adapter.
CommonsWare
The second one seemed the easiest to try first so I made the listview "list" into a global declaration and tried accessing it from the handler, list.setAdapter(searchAdapter); and it keeps throwing a NullPointerException at android.widget.ListView.measureScrapChild(ListView.java). I'm currently seeing what's wrong here... Will get back soon if it works...
Legend
Great... As always... you're the expert when it comes to typical problems like this.. Thank You!!
Legend