Jarvis Millan Jarvis Millan - 1 year ago 118
Android Question

RecyclerView duplicates every item

I have a problem dealing with recyclerview, when i return back to the activity after opening another activity, it duplicates all the items i displayed. Is there any way to fix this?

Like on list view, it just automatically clears the list and then display it again. can you show me how? thanks


int x = 0;
for(String Name : property_name){
JSONObject jsonObject = arrayResponse.getJSONObject(x);
property_id[x] = jsonObject.getString("property_id");
property_name[x] = jsonObject.getString("property_name");
type[x] = jsonObject.getString("property_type");
price[x] = jsonObject.getString("price");
address[x] = jsonObject.getString("address");

ListingNearby listingNearby = new ListingNearby(property_id[x], property_name[x], type[x], price[x], Math.round(distance[x]));


recyclerView = (RecyclerView) findViewById(R.id.ln_recycler_view);
layoutManager = new LinearLayoutManager(this);
adapter = new ListingNearbyAdapter(list);

I wonder what to put in onResume so that the listview clears the list and then display it again.

Answer Source

Just try to list.clear() before you begin your loop. I guess your list is kind of static variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download