我正在使用AsyncTask
並異步獲取twitter feed以顯示在listView中。它工作正常然後,刷新列表視圖我把asyncTask調用到Runnable
的Run()
加載它在一個特定的時間間隔之後。 Feed在特定時間間隔後加載,但listview未更新。我在asyncTask中使用notifyDataSetChanged()
,但它不起作用。如何在AsyncTask中刷新ListView?
這是我試圖代碼:
public class Twitter7FeedActivity extends Activity {
LazyAdapter adapter;
ListView list;
JSONObject jsonObj = null;
JSONArray jsonArray = null;
@SuppressLint("SimpleDateFormat")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_twitter7_feed);
callAsynchronousTask();
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new GetFeedTask().execute(CommonUtils.BEARER_TOKEN,
CommonUtils.URL);
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000);
}
protected class GetFeedTask extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(
Twitter7FeedActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected String doInBackground(String... params) {
// related code
}
@Override
protected void onPostExecute(String jsonText) {
// related code
adapter = new LazyAdapter(Twitter7FeedActivity.this,
// tweets);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
使用通知適配器方法 – KOTIOS
他已經在使用'adapter.notifyDataSetChanged();' –
可能是你想試試這個解決方案嗎? http://stackoverflow.com/questions/15472539/refresh-spinner-view-after-execute-asynchronous-task-in-another-class – tsohtan