Android:如何定期更新ListView?
作者:互联网
我从来没有以直截了当的方式工作.对不起,如果我有点模糊.我会试着详细说明我要做的事情.我正在尝试构建一个从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();
}
}
}
无论我做什么,都会调用处理程序并获取新项目,但列表永远不会刷新…有什么可能出错的想法?
解决方法:
好吧,跟随你的代码有点困难,因为你只有一个片段,而且几个真正相关的位.例如,根据您的可用代码,您的列表应该永远为空,因为您从未将searchAdapter与ListView相关联…至少在您显示的代码中.
话虽如此,以下几行似乎特别奇怪:
searchAdapter = getFeed(URL);
searchAdapter.notifyDataSetChanged();
我将假设getFeed()(未显示)创建某种新的ListAdapter.如果getFeed()正在创建一个新的ListAdapter,则不需要在其上调用notifyDataSetChanged(),因为它的数据集没有改变 – 它是全新的.此外,除非您将此新ListAdapter与ListView相关联,否则新的ListAdapter将不起作用.
如果我正在咆哮错误的树,请考虑在示例中添加行,显示getFeed()的实现以及您使用searchAdapter的位置.
标签:android,refresh,listview,auto-update 来源: https://codeday.me/bug/20190627/1300860.html