How to update items in RecyclerView when a record is deleted, updated or added?

0

I'm working with the detail master pattern, when I'm in landscape mode the list and detail is displayed (it should be noted that in the detail you could call a fragment that contains the logic to add a new record), that's where it goes the problem when you delete a record in landscape mode, it shows the deleted message but it does not update the recyclerview items, like when I'm on a smartphone to see the detail I open another activity that contains the detail where I have the option to delete the registration but when I return the record is still on the list.

  

Also how to get the position of the item removed from the activity

This is the function found on the adapter.

void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

in the click event of the delete button that is in the activity

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_eliminar:
                if(mFragment != null){
                    //Como obtener la posicion del recyclerview - este número es solo de ejemplo
                    mFragment.eliminar(1);
                }
                break;
        }
    }
  

How can I get the position for the delete function, from the activity.

I would like you to give me an explanation of how to achieve this goal.

This is my first question about this problem update items from recyclerview , I leave the code I have achieved with the help of David a great developer.

Code hosted on Gist GitHub

    
asked by Luis Rene Mas Mas 22.08.2017 в 23:30
source

3 answers

2

The recyclerViews behave differently from the lists and therefore should not be compared, in fact they are quite different in operation. First of all, I do not recommend using the method, notifyDataSetChanged. In the same documentation of android talk about the problems of using it, because the indexes in the recycler view are dynamic, that is, they vary according to the information that is displayed on the screen. The correct solution is a bit more complicated than just obtaining the index of the deleted element. What I would do is the following.

  • Starting from the fact that you have two parts, one is your recyclerView (which may or may not be a fragment) and the second is your detail view where the delete button is hosted. You must have somewhere an arrangement, a list something like
  • List<Object> data.
    
  • When you inflate the detail list, you must have an OnItemClick method, in the recycler, send that position to the detail view, and when you give the delete button, send that position through an interface to the acitivity that implements it, then Removes the object from the fix in the selected position.
  • When you do, create a new adapter for the recyclerView, with data already without the object removed, and use the setAdapter (data) method again to load the info with one less element.
  • It's a bit long, but it's the price to pay for the performance of the recyclerView. Good luck

        
    answered by 25.08.2017 в 16:34
    0

    I'm working With reclyclers, well, I'm working with Lists, but every time I add or Delete in the List, I have to re-pass the list through the Adapter in the activity so I can update the Recycler.

        
    answered by 23.08.2017 в 02:28
    0

    How about, I'd like to share an answer that I've made in this link

    Here I explain more or less how a Recycler works. For what you comment I can imagine that you make the change of your database, but it is not updated, if so, the problem has to do with the adapter of your RecyclerView, to update it is as simple as putting:

    nombreAdaptador.notifyDataSetChanged();
    
        
    answered by 23.08.2017 в 02:39