![](/img/trans.png)
[英]onDataChange not called when trying to retrieve data from Firebase Realtime Database
[英]Trying to retrieve data from another firebase database
我正在嘗試從另一個 firebase 數據庫中檢索數據,但我什么也沒顯示。 我沒有收到任何錯誤,我的應用程序沒有崩潰,這是一件好事,但它沒有顯示任何數據並且數據庫中有數據。
有人可以幫我解決這個問題嗎?
recyclerView.setLayoutManager(layoutManager);
myUploads = new ArrayList<Model_Information>();
aAdapter = new Adapter1(getContext(), myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
DatabaseReference databaseReference = FirebaseDatabase.getInstance("https://boot0-43081.firebaseio.com/").getReference("Posts");
if (InternetConnection.checkConnection(getContext())) {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Model_Information upload=postsnapshot.getValue(Model_Information.class);
myUploads.add(upload);
recyclerView.invalidate();
}
linearLayoutWithoutItems.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
aAdapter.notifyDataSetChanged();
}else{
linearLayoutWithoutItems.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
Json
{
"Posts" : {
"37trzrENv0WGEgHWTPvKZ08wWcY2" : {
"-MT1gIzSVIG4cF_ipquI" : {
"created" : "2-09-2021",
"headline" : "Danny Dinner",
"id" : "-MT1gIzSVIG4cF_ipquI",
"mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/boot0-43081.appspot.com/o/37trzrENv0WGEgHWTPvKZ08wWcY2%2F1612805391640.jpg?alt=media&token=9e819b43-507d-4c07-91cb-c006e16a8def",
"main_id" : "-MT1gIzSVIG4cF_ipquI",
"time" : "1:29 AM",
"timestamp" : 1612805393601,
"views" : 0,
"websiteurl" : "www.danny.com"
}
}
},
從外觀上看,您的Posts
節點有一個鍵,其中包含用戶的 UID,然后在下面有該用戶的帖子列表。
由於您的代碼讀取整個Posts
節點,因此它需要在此處處理兩個嵌套級別:用戶和帖子。 所以你需要兩個嵌套循環:
DatabaseReference databaseReference = FirebaseDatabase.getInstance("https://boot0-43081.firebaseio.com/").getReference("Posts");
if (InternetConnection.checkConnection(getContext())) {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot postsnapshot : userSnapshot.getChildren()) {
Model_Information upload=postsnapshot.getValue(Model_Information.class);
myUploads.add(upload);
recyclerView.invalidate();
}
}
linearLayoutWithoutItems.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
aAdapter.notifyDataSetChanged();
}else{
linearLayoutWithoutItems.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
或者,如果您只想閱讀當前用戶的帖子,那就是:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference databaseReference = FirebaseDatabase.getInstance("https://boot0-43081.firebaseio.com/")
.getReference("Posts").child(uid);
然后你可以保持onDataChange
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.