简体   繁体   中英

notifyDataSetChange don't work properly on custom adapter

I'm trying to add some item to db, add on success to reload my listview in a Fragment.

Actually I do have something like that.

  db.collection("quizz").document().set(Quizz).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(context, getString(R.string.quizz_done), Toast.LENGTH_SHORT).show();
                            Quizz nQuizz = new Quizz(""+items.size()+1,""+mScore,uid,now);
                            items.add(nQuizz);
                            adapterquizz.notifyDataSetChanged();

                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(context,getString(R.string.quizz_not_done), Toast.LENGTH_SHORT).show();
                        }
                    });

U only update the list when u init new instance of your adapter. U need to create an update function inside your adapter:

public void updateData(List<Quizz> yourNewList) { data.clear(); // remove this if u just want to insert new item instead of clear all data.addAll(yourNewList) notifyDatasetChange() }

Call this from activity when u need to update data.

You don't need to pass the list to your adapter constructor anymore.

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