-1

how to update Recylerview , after setOnClickListener in adapter

bDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Boolean succes = dbDatasource.delete_ayat_bookmark((qurans.get(position).getnSurat()), qurans.get(position).getnAyat());
                    if (succes) {
                        Message message = new Message();
                        message.pesan_surat_ayat(context, "hapus", (qurans.get(position).getnSurat() + 1), qurans.get(position).getnAyat());
                        adapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                }
            });

logcat :

Process: com.ideabrains.alquranindonesia, PID: 17401
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$Adapter.notifyItemRemoved(int)' on a null object reference
                                at com.ideabrains.alquranindonesia.Bookmark_Adapter$1$4.onClick(Bookmark_Adapter.java:187)
                                at android.view.View.performClick(View.java:5637)

adapter is null, how to get adapter in RecyclerView adapter ?

Annie Aye Myat
  • 227
  • 2
  • 7
Rona Idea
  • 6,734
  • 2
  • 9
  • 12
  • I think is your adapter is null when you call the method. – Crammeur Jul 23 '18 at 01:52
  • thx for answer but @crammeur how get adapter in RecyclerView adapter ? – Rona Idea Jul 23 '18 at 01:58
  • You can create an Custom Adapter extend BaseAdapter or ArrayAdapter is the standard. see this doc this can help you to start https://developer.android.com/guide/topics/ui/layout/recyclerview and this https://developer.android.com/guide/topics/ui/binding – Crammeur Jul 23 '18 at 02:03
  • call getAdapter() or keep a reference when you set the adapter to the recyclerview. – Neo Shen Jul 23 '18 at 02:42
  • In `adapter class` You Don't need to get `adapter object` to call `notifyDataSetChanged();` You can simply write `notifyDataSetChanged();` it works without `object` – Sukhbir Jul 23 '18 at 04:21

3 Answers3

1

Confirm that adapter is initialized and set adapter to RecyclerView before you set notifyDataSetChanged().

And then, check with this code

if(adapter!=null){ 
  adapter.notifyDataSetChanged();
}
Annie Aye Myat
  • 227
  • 2
  • 7
0

Check this doc from google developer site

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#getAdapter()

i think u should confirm adapter is not null before calling notifyDataSetChanged()

timmyB
  • 39
  • 5
0

You need to check your adapter is null or not, then do notifyDataSetChanged();

you can make adapter object of your list adapter class and pass data to it when your activity starts, then onclicklistener just add this

if(adapter!=null)
 { 
  adapter.notifyDataSetChanged();
 }
unzila
  • 190
  • 1
  • 12