How to Stop the Items Duplication in Recyclerview Android

how to stop the items duplication in recyclerView android

Do this

int i = 0;
mArrayList.clear(); //Clear your array list before adding new data in it
for (String name : mNames) {
Data data = new Data(name, mPrices[i], mImages[i]);
mArrayList.add(data);
i++;
}

By doing this your data won't duplicate anymore.
Try it and let me know if it work for you.

How to stop Recyclerview from producing duplicates and stop shuffling items?

So i found the answer finally ,the solution to stop recycler view from duplicating data is by removing the views from your layout before setting data to adapter each time.This worked for me .
example:

layoutManager.removeAllViews()

And never forget to call notifyDataSetChanged after retrieving data to adapter .
Hope it helps others too.

How to prevent RecyclerView Items duplication onBack pressed and coming to Fragment where RecyclerView is set

Just clear you array list before json parsing. I am editing your code. Just add one line.

if(current!=null && current.size()>0){
current.clear();
}


JsonArrayRequest req = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try
{
for( int i=0;i<response.length();i++)
{
JSONObject obj = response.getJSONObject(i);
final BookEntry tableEntry=new BookEntry();
String status=obj.getString("status");
if(status.equals("F"))
{
Entry.setName(obj.getString("name"));
Entry.setDescription(obj.getString("description"));
Entry.setCapacity(obj.getString("capacity"));
Entry.setId(obj.getString("id"));
current.add(tableEntry);
adapter=new BookingAdapter(current,getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}

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

}

RecyclerView is duplicating items

I replicated your problem.

adding holder.priceGroup.removeAllViews(); on onBindViewHolder will fix it. Like so:

 @Override
public void onBindViewHolder(ChecklistAdapter.ViewHolder holder,
int position) {
Checklist packageModel = packageList.get(position);
holder.packageName.setText(packageModel.getTitle());

int id = (position+1)*100;
holder.priceGroup.removeAllViews();

for(String price : packageModel.getQuestions()){
RadioButton rb = new RadioButton(ChecklistAdapter.this.context);
rb.setId(id++);
rb.setText(price);
holder.priceGroup.addView(rb);
}
}

You were adding to the view every time without removing the previous views.

Deleting an item in recycler view duplicates the other item

First delete from the list and then call notifyItemRemoved. Check below.

override fun onClick(dialog: DialogInterface?, which: Int) {
(activity as MainActivity).db.myDao().deleteData(users[position])

users.removeAt(position) // Delete from list
notifyItemRemoved(position)
}

N.B: Your list should be MutableList instead of List to perform the operation.

RecyclerView duplicates every item

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

Android: Item Duplication with RecyclerView & CardView - How do I stop the duplication?

if i got it correct than try to put it into populateData()

if(data!=null) data.clear();

How to fix duplication of item when deleted from RecyclerView?

Looks like you are not updating the mCursor that you are using in your onBindViewHolder function. Though you are updating the mListNotes, you are still populating the data in each item of your RecyclerView from the cursor and the cursor is not being updated when you are deleting an item from the list.

I would rather recommend getting the data from your ArrayList (i.e. mListNotes) instead of the cursor in your onBindViewHolder. Modify the constructor like the following.

// Remove the mCursor completely.
// Looks like you do not need that.
public NotesAdapter(Context ctx, ArrayList<Note> listNotes){
this.mCtx = ctx;
this.mListNotes = listNotes;
}

And in your onBindViewHolder take the data from the mListNotes.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

String noteT = mListNotes.get(position).getTitle();
String noteC = mListNotes.get(position).getContent();

holder.noteTitle.setText(noteT);
holder.noteDescription.setText(noteC);
holder.noteCard_settingsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mDbAdapter = new NotesDbAdapter(mCtx);
mDbAdapter.open();
Toast.makeText(mCtx,"The position is:"+holder.getAdapterPosition(),Toast.LENGTH_SHORT).show();
PopupMenu popup = new PopupMenu(mCtx, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_popup, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.popup_edit:
Toast.makeText(mCtx,"You pressed edit note", Toast.LENGTH_SHORT).show();
return true;
case R.id.popup_delete:
deleteItem(holder.getAdapterPosition());
Toast.makeText(mCtx, "Delete note", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
popup.show();
}
});
}

I hope that fixes your problem.

In case you want to keep the cursor in your adapter, then make sure the cursor passed to the adapter is updated as well.



Related Topics



Leave a reply



Submit