0

我正在使用CustomAdapterwhich extendsBaseAdapterAsynccTask显示结果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 后,列表视图会在最后附加新项目,但我需要只使用新项目完全刷新列表视图。

4

2 回答 2

2

在将新项目放入 ListView 之前,您首先需要清除已在 ListView 中的项目。

例如:

    clearButton.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            adapter.clear();
            adapter.notifyDataSetChanged();
        }
    });
}
于 2013-07-25T13:50:09.797 回答
1

由于声誉较低,我给你答案,否则可以在评论中给出。

adapter.notifyDataSetChanged();当您完成后台任务或更新数据时使用 。

于 2013-07-25T13:56:28.060 回答