Kaidul
Kaidul

Reputation: 15885

My ListView is updating but not removing the previous items

I am using CustomAdapter which extends BaseAdapter and AsynccTask to show results in a ListView. Everything works well but I want to refresh the listView with new items every 10 minutes later, so I put the AsyncTask inside a TimeTask. The problem is the listview is not removing it's old items rather it appends thee new items with old one. I use notifyDataSetChanged() and all the stuffs I found on internet but nothing happens. This is my trying code:

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();
        }
    }
}

And this is my LazyAdapter class:

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;
    }
}

After running the AsyncTask again again, the listview append new items at the end, but I need to completely refresh the listview with only new items.

Upvotes: 0

Views: 1728

Answers (2)

Sameer
Sameer

Reputation: 114

Due to less Reputations i am giving you answer else it can be given in comment.

use adapter.notifyDataSetChanged(); when you done with background task or updating data.

Upvotes: 1

DarkBladeX
DarkBladeX

Reputation: 40

Before you put your new items in the ListView you first need the clear the ones that are already inside the ListView.

For example:

    clearButton.setOnClickListener(new OnClickListener()
    {

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

Upvotes: 2

Related Questions