我正在使用CustomAdapter
which extendsBaseAdapter
并AsynccTask
显示结果ListView
. 一切正常,但我想每 10 分钟后用新项目刷新 listView,所以我把AsyncTask
里面的TimeTask
. 问题是listview
不是删除它的旧项目,而是将新项目附加到旧项目中。我使用notifyDataSetChanged()
和我在互联网上找到的所有东西,但没有任何反应。这是我的尝试代码:
public class Twitter7FeedActivity extends Activity {
LazyAdapter adapter;
ListView list;
JSONObject jsonObj = null;
JSONArray jsonArray = null;
ArrayList<HashMap<String, String>> tweets = new ArrayList<HashMap<String, String>>();
@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, 30000);
}
protected class GetFeedTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//related code
}
@Override
protected void onPostExecute(String jsonText) {
// My Json parsing related code
list = (ListView) findViewById(R.id.list);
adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets);
list.setAdapter(adapter);
//adapter.notifyDataSetChanged();
((LazyAdapter) list.getAdapter()).notifyDataSetChanged();
}
}
}
这是我的 LazyAdapter 类:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
notifyDataSetChanged(); // I am also using it here
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView) vi.findViewById(R.id.name);
TextView tweet = (TextView) vi.findViewById(R.id.text);
TextView date = (TextView) vi.findViewById(R.id.created_at);
//related code
return vi;
}
}
再次运行 AsyncTask 后,列表视图会在最后附加新项目,但我需要只使用新项目完全刷新列表视图。