Android:如何定期更新ListView?

时间:2010-02-04 00:49:16

标签: android listview refresh auto-update

我从来没有以直截了当的方式工作。对不起,如果我有点模糊。我会试着详细说明我要做的事情。我正在尝试构建一个从Web服务中获取数据的列表视图。初始化listview后,我想定期轮询Web服务器并更新listview的内容。为此,我正在做这样的事情:

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(); 
    }
}

SearchHandler类很简单:

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();
        }
    }
}

无论我做什么,都会调用处理程序并获取新项目,但列表永远不会刷新......有什么可能出错的想法?​​

1 个答案:

答案 0 :(得分:4)

嗯,跟随你的代码有点困难,因为你只有一个片段,而且几个真正相关的位。例如,根据您的可用代码,您的列表应该永远为空,因为您永远不会将searchAdapterListView相关联...至少在您显示的代码中。

话虽如此,以下几行似乎特别奇怪:

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

我将假设getFeed()(未显示)创建某种新的ListAdapter。如果getFeed()正在创建新的ListAdapter,则无需在其上调用notifyDataSetChanged(),因为其数据集尚未更改 - 这是全新的。此外,除非您将新的ListAdapterListView相关联,否则新的ListAdapter将无效。

如果我正在咆哮错误的树,请考虑在示例中添加行,以显示getFeed()的实施情况以及使用searchAdapter的位置。