androidandroid-recyclerviewandroid-adapterandroid-adapterview

How to update recyclerview when an activity is deleted which is open from recyclerview adapter


I am using Recyclerview adapter to populate Recyclerview. After populating Recyclerview from SQLite, If user want to open an recyclerview item need to click on that item and adapter open the related activity. Here is an image which can help you understand easily.

Updating RecyclerView

When an activity is open user can delete that post from SQLite by clicking delete button after deleting data recyclerview should dynamically update data.


Solution

  • You can also use StartActivityForResult and use the result of the second activity for delete item in first one.

    I mean:

    1. FirstActivity starts SecondActivity waiting for result
    2. SecondActivity sends the result back to FirstActivity. Only if you delete the item.
    3. Now FirstActivity remove and refresh the list.

    In FirstActivity:

    Intent i = new Intent(this, SecondActivity.class);
    startActivityForResult(i, 1);
    

    In SecondActivity, when you push delete button:

    Intent returnIntent = new Intent();
    returnIntent.putExtra("delete", true);
    returnIntent.putExtra("position", position);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();
    

    And finally, FirstActivity handle the result:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                if (data.getBooleanExtra("delete") {
                     // get position and delete item from list and refresh
                     int position = data.getIntegerExtra("position");
                }
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }//onActivityResult
    

    https://stackoverflow.com/a/10407371/1820599

    Edited:

    Getting the context of your activity inside the adapter constructor:

    FirstActivity listener;
    
    public myAdapter(Context context, List<String> items) {
            super(context, R.layout.row_edition, items);
    
            this.listener = ((FirstActivity) context);
            this.items = items;
        }
    

    Then, inside the adapter, when you push on item, call the activity to start the seconde one:

    listener.startSecondActivity(int position, parameters you need to use);
    

    and finally, in your FirstActivity

    startSecondActivity(int position, parameters you need to use) {
        // whatever you have to do
        Intent i = new Intent(this, SecondActivity.class);
        // push position inside intent and whatever you need
        startActivityForResult(i, 1);
    }
    

    The flow is:

    1. Push item
    2. Use FirstActivityListener to call SecondActivity
    3. In SecondActivity delete and senr result back
    4. In FirstActivity remove item from adapter, using an auxiliar method inside que adapter