gpt4 book ai didi

java - ValueEventListener.onDataChange() 不会被调用

转载 作者:行者123 更新时间:2023-12-02 13:11:04 26 4
gpt4 key购买 nike

出于某种原因,当我在 fragment 中调用 getTitle() 时,它总是返回 null。似乎根本没有添加 ValueEventListener。

当我在监听器内调试或打印 title 的值时,它不会打印...onCancelled() 也不会打印,但会调用 ValueEventListener。

有谁知道问题出在哪里吗?当我直接在 View 中使用 ChildEventListener 时,它工作正常,所以它不是数据库连接。

JSON 对象:

"about" : {
"info" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In non suscipit risus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse in lectus consectetur nisl molestie luctus. Nulla commodo tincidunt dui ut dignissim. Suspendisse sapien lacus, tristique non ultricies nec, accumsan nec ipsum. Integer feugiat faucibus eros. Mauris augue ex, tincidunt a elit nec, pharetra aliquam nisl. Quisque viverra fringilla pulvinar. Nulla et vehicula ipsum, sed ultrices est. Maecenas bibendum ipsum quis erat cursus euismod. Nullam tempor diam eu eros ultrices, vel efficitur lacus bibendum. Cras auctor erat sem, quis auctor libero feugiat sit amet. Fusce semper odio at lacus faucibus, a feugiat massa suscipit. Pellentesque id est id massa faucibus faucibus quis eget libero.",
"title" : "Vårt hotell er best i hele verden"
},

问题类别:

public class AboutUsObserver extends AboutUs implements MyObserverInterface {

private List<DataChangedListener> listeners = new ArrayList<>();
public AboutUsObserver(Query mRef){
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()){
if (child.getKey().equals("title")) {
String title = child.getValue(String.class);
setTitle(title);
}

else if (child.getKey().equals("info")) {
String info = child.getValue(String.class);
setInfo(info);
}
}

for (DataChangedListener listener : listeners)
listener.onDataChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

@Override
public void addDataChangedListener(MyObserverInterface.DataChangedListener dataChangedListener) {
listeners.add(dataChangedListener);
}

@Override
public void removeListenerAt(int index) {
listeners.remove(index);
}

@Override
public void removeListeners() {
listeners = new ArrayList<>();
}

}

关于我们类:

public class AboutUs {

public String title;
public String info;

// Required default constructor for Firebase object mapping
@SuppressWarnings("unused")
public AboutUs(){}

public AboutUs (String title, String info) {
this.title = title;
this.info = info;
}

public void setTitle(String title){
this.title = title;
}

public void setInfo(String info){
this.info = info;
}

public String getTitle(){
return title;
}

public String getInfo(){
return info;
}
}

在 fragment onCreateView() 中:

DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference aboutRef = mRef.child("about");

final TextView TITLE = (TextView) view.findViewById(R.id.aboutTitle);
final TextView INFO = (TextView) view.findViewById(R.id.aboutInfo);

final AboutUsObserver aboutUsObserver = new AboutUsObserver(mRef);

aboutUsObserver.addDataChangedListener(new MyObserverInterface.DataChangedListener() {
@Override
public void onDataChanged() {
String test = aboutUsObserver.getTitle();
TITLE.setText(aboutUsObserver.getTitle());
INFO.setText(aboutUsObserver.getInfo());
}
});

最佳答案

AboutUsObserver 预计子级 titleinfo 会发生更改。它应该监听位置 about 的更改。但是您正在初始化它以监听根位置:

DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
...
final AboutUsObserver aboutUsObserver = new AboutUsObserver(mRef);

更改为:

final AboutUsObserver aboutUsObserver = new AboutUsObserver(aboutRef );

关于java - ValueEventListener.onDataChange() 不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43960352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com