我试图每秒钟更新一次列表视图,所以我使用了处理程序和线程来更新
我用notifyDataSetChanged();
重新加载listview
而不刷新活动。
但我的列表视图没有更新。
如何在不刷新活动的情况下重复更新我的列表视图
private class MessageTask extends AsyncTask<String, Void, String> {
private final HttpClient Client = new DefaultHttpClient();
@Override
protected String doInBackground(String... urls) {
String output = "";
for (String url : urls) {
try{
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
output = Client.execute(httpget, responseHandler);
}catch(Exception e){
Log.i("Animation", "Thread exception " + e);
}
}
return output;
}
protected void onPostExecute(String output) {
try {
JSONObject jObject= new JSONObject(output);
JSONArray menuObject = new JSONArray(jObject.getString("response"));
ArrayList<String> tExp=new ArrayList<String>();
//HashMap<String,ArrayList> map = new HashMap<String,ArrayList>();
for (int i = 0; i<menuObject.length(); i++)
{
tExp.add(menuObject.getJSONObject(i).getString("fk_username_c").toString()+" "+menuObject.getJSONObject(i).getString("message_c").toString());
}
adapter=new ArrayAdapter<String>(ChatActivity.this,android.R.layout.simple_list_item_1);
adapter.addAll(tExp);
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {
@Override
public void run() {
adapter.setNotifyOnChange(isChangingConfigurations());
handler.postDelayed( this, 1000 );
}
}, 1000 );
listview.setAdapter(adapter);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
//intent.putExtra(extras);
}
}