我的代码如下所示:
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
private static final String TAG = "OrderManagementFragment";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_managment, container, false);
//get reference to the orders node
mFirebaseDatabase = mFirebaseInstance.getReference("orders");
// app_title change listener
mFirebaseInstance.getReference("58ca237b2e2c211dc0c7ed9b").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(getActivity(), dataSnapshot.getValue(String.class), Toast.LENGTH_LONG).show();
Log.d(TAG, "onDataChange: " + dataSnapshot.getValue(String.class));
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
Log.d(TAG, "onCancelled: " + error.getMessage());
}
});
return view;
}
我的firebase凭据如下所示:
现在,这里发生的是,每当从后端更改数据时,onDatachange方法永远不会被调用。这个方法中是否有任何缺失?
我是通过Android模拟器测试的,所以问题是什么?
答案 0 :(得分:2)
我改变了你的代码,你不需要引用构造函数因为aplication方法会知道你需要什么。
因为您的布局上没有类似textView的静态数据,因此不会更改数据。当你有一个Toast,它的开始一次,所以你不能看到它改变了
//get reference to the orders node
mFirebaseDatabase = mFirebaseInstance.getReference();
// app_title change listener
mFirebaseInstance.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(getActivity(), dataSnapshot.child("YOUR REFERENCE HERE")getValue(String.class), Toast.LENGTH_LONG).show();
}
我从我的申请中给你举例:
mDatabase = FirebaseDatabase.getInstance().getReference();
settingsText2.setText("Loading...");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
settingsTextWithScrollView.setText(dataSnapshot.child("Youtube1").child("Text").getValue(String.class));
settingsText2.setText(dataSnapshot.child("Youtube1").child("Information").getValue(String.class));
settingsTextWithScrollView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openYoutubeIntent(dataSnapshot.child("Youtube1").child("Link").getValue(String.class));
}
});
settingsText3.setText(dataSnapshot.child("Youtube2").child("Text").getValue(String.class));
settingsText4.setText(dataSnapshot.child("Youtube2").child("Information").getValue(String.class));
settingsText3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openYoutubeIntent(dataSnapshot.child("Youtube2").child("Link").getValue(String.class));
}
});
settingsText5.setText(dataSnapshot.child("Youtube3").child("Text").getValue(String.class));
settingsText6.setText(dataSnapshot.child("Youtube3").child("Information").getValue(String.class));
settingsText5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openYoutubeIntent(dataSnapshot.child("Youtube3").child("Link").getValue(String.class));
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 1 :(得分:0)
这是一个像这样的静态调用
FirebaseDatabase database=FirebaseDatabase.getInstance();
DataBaseRefrence dbRef=database.getRefrence("nodeOfData");
DataSnapshot ds=dbRef.getChildren("gettingWhatIsInNodeOfData"); or
better yet chain them DataSnapshot
ds=FirebaseDatabase.getInstance().getRefrence("orders").getChildren();