RecyclerView creates a ghost record when deleting

2

I have a problem removing an item from my RecyclerView (which is linked to a database SQLite ). After successfully removing the data from both the database and the view, the animation is shown as deleted, but then a new element appears at the end, also with a nice animation.

When checking the database I see that the record in question is deleted.

I could solve this by instantiating a new adapter every time I delete a record, but then the animation is not displayed.

Code:

Adapter method:

public void removeItem(int position){
    db = new DBAdapter(context);
    db.open();
    mCursorAdapter.getCursor().moveToPosition(position);
    int idnota = mCursorAdapter.getCursor().getInt(0);
    db.borra_nota(idnota); //it deletes the row (delete_note)
    db.close();
}

Main Activity :

public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    //if (direction == ItemTouchHelper.LEFT) {

    listAdapter.removeItem(position);
    listAdapter.notifyItemRangeChanged(position, listAdapter.getItemCount());
    listAdapter.notifyItemRemoved(position);

The seedy solution (without animation) happened to be something like this:

public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    int position = viewHolder.getAdapterPosition();
    sticky = (RecyclerView) findViewById(R.id.lista_notas);
    linearLayoutManager = new LinearLayoutManager(MainActivity.this);
    sticky.setLayoutManager(linearLayoutManager);

    listAdapter = new ListAdapter(MainActivity.this);
    sticky.setAdapter(listAdapter);

In which case, I would pass the removeItem and so on within the Adapter .

Any solution so that when the record is deleted, the animation is displayed correctly and also does not create a ghost record at the end?

    
asked by dragonkhanrider 27.01.2017 в 17:48
source

1 answer

0

Ok, solved ...

The problem was that I was removing from all sites except the CursorAdapter itself.

The ListAdapter method is now like this, working correctly:

    public void removeItem(int position){
        db = new DBAdapter(context);
        db.open();
        mCursorAdapter.getCursor().moveToPosition(position);
        int idnota = mCursorAdapter.getCursor().getInt(0);
        db.borra_nota(idnota); //it deletes the row (delete_note)

        items = db.consulta_todos(); //select all from db again
        mCursorAdapter.changeCursor(items);
    }

Greetings, I hope it is useful to anyone who has run into my problem.

    
answered by 27.01.2017 в 18:18