简体   繁体   English

FirebaseDatabase 和 FirebaseFirestore 数据处理和检索数据的问题

[英]Issue with FirebaseDatabase and FirebaseFirestore Data Manupulate and Retriving data

My query is that I want to save some data in Firebase Realtime Database such that I will be able to call the data.我的查询是我想在 Firebase 实时数据库中保存一些数据,以便我能够调用这些数据。 Keeping in mind that users should be able to call the data even if they reinstall the app and each user will have a specific child.请记住,即使重新安装应用程序,用户也应该能够调用数据,并且每个用户都会有一个特定的孩子。

I am trying to make an app where the user will enter or save data to firebase and display it on the App, however, the issue is that I am not having an Idea on how to retrieve the data from firebase.我正在尝试制作一个应用程序,用户将在其中输入或保存数据到 firebase 并将其显示在应用程序上,但是,问题是我没有关于如何从 firebase 检索数据的想法。

The data which I am going to retrieve is the ItemName which I have saved inside我要检索的数据是我保存在里面的 ItemName

child(uid).child(ItemName); child(uid).child(ItemName);

each user will save their data with different uid inside different ItemName.每个用户将在不同的 ItemName 中使用不同的uid保存他们的数据。

The thing is that how will I retrieve the ItemName for each user since the ItemName will be different so I will not have any data of the ItemName for a particular user.问题是我将如何检索每个用户的 ItemName,因为 ItemName 会不同,所以我不会有任何特定用户的 ItemName 数据。 If I save the ItemName in SharedPreferences I will be able to retrieve it, however, if the user, formate their phone or uninstall and reinstall the app then SharedPreference data will be removed, SO HOW WILL I RETRIVE THE ITEMNAME data from Firebase for a particular user to display it.如果我将 ItemName 保存在 SharedPreferences 中,我将能够检索它,但是,如果用户格式化他们的手机或卸载并重新安装应用程序,那么 SharedPreference 数据将被删除,那么我将如何从 Firebase 中检索特定的 ITEMNAME 数据用户来显示它。

MY CODE example:我的代码示例:

String email= mAuth.getCurrentUser().getUid();
dbFirestore= FirebaseFirestore.getInstance();
userReference= FirebaseDatabase.getInstance().getReference().child("Users").child(itemName);

在此处输入图像描述

Is there any method or function which retrieve the data without specifying the Child pathname like default path if we redirect the path towards there like如果我们将路径重定向到那里,是否有任何方法或 function 检索数据而不指定默认路径之类的子路径名

databaseReference = FirebaseDatabase.getInstance().getReference().child(uid).child(""); 

something like that.类似的东西。 or is there any other way to do it.或者有没有其他方法可以做到这一点。

在此处输入图像描述

To those values, without knowing the name of the intermediate node, please use the following lines of code:对于这些值,在不知道中间节点名称的情况下,请使用以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String friedChicken = ds.getKey();
            String foodType = ds.child("food_TYPE").getValue(String.class);
            Log.d("TAG", friedChicken + "/" + foodType);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

The output in the logcat will be: logcat 中的 output 将是:

Fried Chicken / Non-Veg

In the same way I got the value of food_TYPE , you can also get the others.同我得到food_TYPE的值一样,你也可以得到其他的。

You can do the following您可以执行以下操作

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(uid).child("Fried Chicken");
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String foodType = dataSnapshot.child("food_TYPE").getValue().toString();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM