Jarvis Millan Jarvis Millan - 15 days ago 5
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


OnCreate


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]));
x++;
list.add(listingNearby);

}

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


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

Answer

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