作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Firebase valueEvent 监听器:
questions.orderByChild("CategoryID").equalTo(categoryID)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
//Random List
Collections.shuffle(Common.questionList);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
作为拥有可以附加和删除的对象的最佳实践,我想将代码更改为 ValueEventListener 对象。我做了以下事情:
mQuestionsListener = questions.orderByChild("CategoryID").equalTo(categoryID)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
//Random List
Collections.shuffle(Common.questionList);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
questions.addValueEventListener(mQuestionsListener);
但不工作。我哪里错了?
最佳答案
您已经在此处添加了一个监听器questions.addValueEventListener(mQuestionsListener);
,那么您需要声明一个新的ValueEventListener()
:
ValueEventListener mQuestionsListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question ques = postSnapshot.getValue(Question.class);
Common.questionList.add(ques);
}
//Random List
Collections.shuffle(Common.questionList);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
关于java - 如何更改为我可以删除的 ValueEventListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58590264/
我是一名优秀的程序员,十分优秀!