我的ListView正在更新但不删除以前的项目

时间:2013-07-25 13:42:19

标签: android android-listview android-asynctask

我正在使用CustomAdapter扩展BaseAdapterAsynccTask以在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后,listview会在最后添加新项目,但我需要使用新项目完全刷新listview。

2 个答案:

答案 0 :(得分:2)

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

例如:

    clearButton.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            adapter.clear();
            adapter.notifyDataSetChanged();
        }
    });
}

答案 1 :(得分:1)

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

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