4

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.

Harish Kamboj
  • 887
  • 12
  • 32
  • That's fairly simple. Just call `notifyDataSetChanged` on your adapter after you've deleted the entry from your database. The list will refresh itself. – Pztar Aug 23 '16 at 14:58
  • but post can be deleted from another activity – Harish Kamboj Aug 23 '16 at 15:00
  • That's fine, it doesn't matter where it's deleted from. If your `RecyclerView` gets destroyed it'll reload the data when you create it and your old entry will no longer be there. If it doesn't get destroyed then use the existing `RecyclerView` to call `notifyDataSetChanged` – Pztar Aug 23 '16 at 15:03
  • but I want to show data dynamically – Harish Kamboj Aug 23 '16 at 15:04
  • I don't quite understand what you mean then. Your post says you want to dynamically update the data. `notifyDataSetChanged` will help you do that. Perhaps if you post some code it'll be easier to diagnose your issue. – Pztar Aug 23 '16 at 15:10
  • I am not using any code coz i don't know how to handle updating adapter from an activity open from same adapter – Harish Kamboj Aug 23 '16 at 15:46

3 Answers3

2

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
Community
  • 1
  • 1
adalpari
  • 3,052
  • 20
  • 39
  • I am using adapter to open an activity and then update data of that adapter can that be possible with **onActivityResult** in adapter – Harish Kamboj Aug 23 '16 at 15:15
  • Yes, but you have to manage inside your activity code and then reply to the adapter. – adalpari Aug 23 '16 at 15:17
  • ok, I tried this with recyclerview activity but forgot to use **onActivityResult** in adapater – Harish Kamboj Aug 23 '16 at 15:18
  • onActivityResult should go inside the activity, not inside the adapter. The activity will handle the result. – adalpari Aug 23 '16 at 15:21
  • but how to use startActivityForResult in adapter ? – Harish Kamboj Aug 23 '16 at 15:24
  • listener.startSecondActivity(int position, parameters you need to use); how to use this – Harish Kamboj Aug 23 '16 at 15:42
  • @adalPaRi Nice solution. I have a similar question here: https://stackoverflow.com/questions/46046633/recyclerview-how-to-refresh-details-activity I would appreciate any thoughts or ideas you could suggest. – AJW Sep 06 '17 at 03:15
1

if you display list of companies in recyclerview once you click to show detailes of company and you delete the company once back you should found the item disappear this what my code do

 protected void onResume()
{
    super.onResume();
    Log.i("TAG", "resume");
    if(yourlist.size() > 0)
    {
        yourlist.clear();
        yourlist.addAll(where your data come from 
        ex:databaseHelper.GetOrganization());
        youradapter.notifyDataSetChanged();
    }
}
  • 1
    Welcome to SO. Please consider explaining why and how your code help. Code&only answeres are considered to be a bad style – Neuron Nov 15 '17 at 08:43
0

You must have to implement a listener in your activity, that tells your recycler view that the items has changed. I suppose that you have implemented your own onItemClickListener for recycler view, so you have position and can easily remove item from recycler view data set. For more info, please, post your code.

This listener goes in your class that populate Recycler view.

     public interface DeletedListener {
         void deleted(int position);
     }

Makes your activity implement this listener, and there send what position have to remove.

    public void setListener(DeletedListener listener) {
       this.listener = listener;
    }

    DeletedListener listener;

From your activity call the setListener method, and from adapter, call deleted method.