How to update recyclerview when an action is deleted that is open from the recycliewiew adapter

I am using the Recycliewiew adapter to populate the Recyclerview. After filling in the Recyclerview from SQLite, if the user wants to open the recyclerview element, you need to click on this element and the adapter to open the corresponding operation. Here is an image to help you easily understand.

RecyclerView Update

When the activity is open, the user can delete this record from SQLite by clicking the delete button after deleting the data. reyclerview should dynamically update data.

+4
source share
3 answers

StartActivityForResult delete .

:

  • FirstActivity SecondActivity,
  • SecondActivity FirstActivity. .
  • FirstActivity .

FirstActivity:

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

SecondActivity, :

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

, , FirstActivity :

@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 no result
        }
    }
}//onActivityResult

fooobar.com/questions/12443/...

:

:

FirstActivity listener;

public myAdapter(Context context, List<String> items) {
        super(context, R.layout.row_edition, items);

        this.listener = ((FirstActivity) context);
        this.items = items;
    }

, , , :

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

, , 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);
}

:

  • FirstActivityListener SecondActivity
  • SecondActivity delete senr result back
  • FirstActivity ,
+1

recyclerview, , , , , ,

 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

, , . , onItemClickListener recycler, . , , .

, Recycler.

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

Makes your activity an implementation of this listener, and some kind of position is sent there for deletion.

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

    DeletedListener listener;

In your activity, call the setListener method and from the adapter, the remote call method.

0
source

Source: https://habr.com/ru/post/1652204/


All Articles