简体   繁体   中英

notifyDataSetChanged in setOnClickListener RecyclerView Adapter

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 ?

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

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM