Skip to content Skip to sidebar Skip to footer

How To Refresh Recyclerview When I Click The Dialog Button?

I want when I click the alertdialog save button add new item to recyclerview and refresh it to show the new item at the same time .... its take the data from sqlite database.and re

Solution 1:

Once you add the new data to your db, you will have to reset the data in your recyclerView and call notifyDataSetChanged() . You would need a setData method in your recyclerView like so

public void setData(List<YourDataType> data){
   this.data = data;
   notifyDataSetChanged();
 // where this.data is the recyclerView's dataset you are 
 // setting in adapter=new Adapter(this,db.getData());
}

When you add new data, you will have to add the following lines to your code.

if (result == true) {
    Toast.makeText(MainActivity.this, "Add 
                    Successfully",Toast.LENGTH_LONG).show();
    title.setText("");
    description.setText("");
    adapter.setData(db.getData);
    //this will reset your recyclerView's data set and notify the change
    //and reload the list 
}

Solution 2:

In simplest way, you must define your DataList and your adapter as fields and after showing dialog, add new object(s) to your DataList. Then call adapter.notifyDataSetChanged(). It worked for me, but there are some better ways.


Post a Comment for "How To Refresh Recyclerview When I Click The Dialog Button?"