[英]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.您不再需要将列表传递给您的适配器构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.