Android : Refreshing listview adds duplicates

on Saturday, August 2, 2014


I'm using a swipe-down to refresh implementation that calls this on refresh:



public void onRefresh() {
swipeLayout.setRefreshing(true);
swipeLayout.setColorScheme(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeLayout.setRefreshing(false);
Toast.makeText(getApplicationContext(), "The refresh is working!", Toast.LENGTH_SHORT).show();
AppController.getInstance().getRequestQueue().getCache().remove(URL_FEED);
feedRequest();

}
}, 2000);
}


It invalidates the cache and makes a fresh call to the server with Volley (feedRequest).


Here's how I parse the response from that data to be displayed:



private void parseJsonFeed(JSONArray response) {
try {

for (int i = 0; i < response.length(); i++) {
JSONObject feedObj = (JSONObject) response.get(i);

FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));

item.setHeadline(feedObj.getString("headline"));

item.setTimeStamp(feedObj.getString("created_at"));

JSONObject mVendor = feedObj.getJSONObject("vendor");
item.setVendorPic(mVendor.getString("image_url"));

item.setImge(feedObj.getString("image_url"));

feedItems.add(item);
}

listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}


So when I call notifyDSC it adds all of the parsed items into the existing list, including items that might already exist in the list. How can I ensure that I don't end up with duplicates? I thought about wiping the List clean in onRefresh(), but that doesn't seem optimal unless I have to do that. Any ideas on how to solve this problem?


0 comments:

Post a Comment