简体   繁体   中英

Reading From RealtimeDatabase For a Specific Node

My Firebase RealtimeDatabase Contains a Table called Records. In that table there are FirebaseUser Uid's, in each Uid there are dates and in each date there is a list of products. This is an example from the firebase console.

In an activity called DailyTableActivity there is a button prompting to DatePickerDialog and a ListView element which should show the products of a specific date from the dialog (or the current date) for the connected User.

This is class Product

    import java.util.Comparator;

public class Product  {
    public Product() {

    }

    private String id;
    private String Name;
    private double Cal;
    private double Carb;
    private double Prot;
    private double Fat;
    private String Unit;
    private int Def;

    public Product(String name, int cal, int carb, int prot, int fat, String unit, int def,String id) {
        Name = name;
        Cal = cal;
        Carb = carb;
        Prot = prot;
        Fat = fat;
        Unit = unit;
        Def = def;
        this.id=id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getCal() {
        return Cal;
    }

    public void setCal(double cal) {
        Cal = cal;
    }

    public double getCarb() {
        return Carb;
    }

    public void setCarb(double carb) {
        Carb = carb;
    }

    public double getProt() {
        return Prot;
    }

    public void setProt(double prot) {
        Prot = prot;
    }

    public double getFat() {
        return Fat;
    }

    public void setFat(double fat) {
        Fat = fat;
    }

    public String getUnit() {
        return Unit;
    }

    public void setUnit(String unit) {
        Unit = unit;
    }

    public int getDef() {
        return Def;
    }

    public void setDef(int def) {
        Def = def;
    }


    public static Comparator<Product> productComparator= new Comparator<Product>() {
        @Override
        public int compare(Product o1, Product o2) {
            return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
        }
    };
}

This is my code to read data:

public void getRecordOfDate( String date , final DataStatus dataStatus)
{
    final String currId= FirebaseAuth.getInstance().getCurrentUser().getUid();
    date="D_"+date;
    DatabaseReference recordsOfUser=FirebaseDatabase.getInstance().getReference().child("records").child(currId);
    DatabaseReference dateRef=recordsOfUser.child(date);
    dateRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
            if (snapshot.exists()){
                List<String> keys = new ArrayList<>();
                for (DataSnapshot keyNode : snapshot.getChildren()) {
                    products.clear();
                    keys.add(keyNode.getKey());
                    Product product = keyNode.getValue(Product.class);
                    Log.d("DATA_SUCCESS", "Adding product " + product.getName());
                    products.add(product);
                    dataStatus.DataIsLoaded(products, keys);
            }
            }
        }

        @Override
        public void onCancelled(@NonNull @NotNull DatabaseError error) {

        }
    });


}

dataStatus is an interface which works fine, which is why I did not include it here. I think my main problem comes from dates not existing, however even if I choose a date that exists it doesn't work.

I do think it is important to mention that if I choose a date that exists in hard code, data is read, like this:

DatabaseReference dateRef=recordsOfUser.child("D_21_05_2021");

I do appreciate any help and support and I hope you can help me find a solution to this problem, as I am not very familiar with NoSQL databases and I want to explore the possibilities of Realtime.

String public void getRecordOfDate( String date , final DataStatus dataStatus){
final String currId= FirebaseAuth.getInstance().getCurrentUser().getUid();
date="D_"+date;
DatabaseReference recordsOfUser=FirebaseDatabase.getInstance().getReference().child("records").child(currId);
DatabaseReference dateRef=recordsOfUser.child(date);
dateRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
        if (snapshot.exists()){
            List<String> keys = new ArrayList<>();
            if(snapshot.hasChild(date)){
            for (DataSnapshot keyNode : snapshot.getChildren()) {
                //products.clear();
                keys.add(keyNode.getKey());
                //Product product = keyNode.getValue(Product.class);
                String Name = keyNode.child("name").getValue().toString();
                double Cal = Double.parseDouble(keyNode.child("cal").getValue().toString());
                double Carb = Double.parseDouble(keyNode.child("carb").getValue().toString());
                double Prot = Double.parseDouble(keyNode.child("prot").getValue().toString());
                double Fat = Double.parseDouble(keyNode.child("fat").getValue().toString());
                String Unit = keyNode.child("unit").getValue().toString();
                int Def = Integer.parseInt(keyNode.child("def").getValue().toString());
                String Id = keyNode.getKey();
                Product product = new Product(Name, Cal, Carb, Prot, Fat, Unit, Def, Id);
                Log.d("DATA_SUCCESS", "Adding product " + product.getName());
                products.add(product);
                dataStatus.DataIsLoaded(products, keys);
        }
        }else{System.out.print("Date does not exist")}
        }
    }

    @Override
    public void onCancelled(@NonNull @NotNull DatabaseError error) {

    }
});}

Try like this, hope it works! Then you should be able to operate with your list to place data on TextView and that kind of stuff. I commented products.clear() line to save the list on "local" and not delete it on each iteration. If this doesn't work for you, problem must be on your date gather method or here date="D_"+date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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