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(); } }}
无论我做什么,都会调用处理程序并获取新项目,但列表永远不会刷新…有什么可能出错的想法?
解决方法 好吧,跟随你的代码有点困难,因为你只有一个片段,而且几个真正相关的位.例如,根据您的可用代码,您的列表应该永远为空,因为您从未将searchAdapter与ListVIEw相关联…至少在您显示的代码中.话虽如此,以下几行似乎特别奇怪:
searchAdapter = getFeed(URL); searchAdapter.notifyDataSetChanged();
我将假设getFeed()(未显示)创建某种新的listadapter.如果getFeed()正在创建一个新的listadapter,则不需要在其上调用notifyDataSetChanged(),因为它的数据集没有改变 – 它是全新的.此外,除非您将此新listadapter与ListVIEw相关联,否则新的listadapter将不起作用.
如果我正在咆哮错误的树,请考虑在示例中添加行,显示getFeed()的实现以及您使用searchAdapter的位置.
总结以上是内存溢出为你收集整理的Android:如何定期更新ListView?全部内容,希望文章能够帮你解决Android:如何定期更新ListView?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)