jumper
jumper

Reputation: 105

How to display value form Firebase database to the firebase recycle view, with unknowns node?

enter image description here

I want to display value from Firebase database into a RecycleView but the problem is, there unknown node need to defined at reference to get the value.

Sorry for my bad English.

public FirebaseRecyclerOptions setFirebaseRecyclerOptions(FirebaseRecyclerOptions<DataViewHistory> firebaseRecyclerOptions) {
        FirebaseUser user = mAuth.getCurrentUser();
        this.firebaseRecyclerOptions = firebaseRecyclerOptions;
        this.firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DataViewHistory>().setQuery(databaseReference.child("purchaseHistory").child(user.getUid()), DataViewHistory.class).build();
        return this.firebaseRecyclerOptions;
    }

public void setFirebaseRecyclerAdapter(FirebaseRecyclerAdapter<DataViewHistory, ViewHolderHistory> firebaseRecyclerAdapter) {
        this.firebaseRecyclerAdapter = firebaseRecyclerAdapter;
        this.firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DataViewHistory, ViewHolderHistory>(setFirebaseRecyclerOptions(firebaseRecyclerOptions)) {


            @NonNull
            @Override
            public ViewHolderHistory onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cartlist, parent, false);
                return new ViewHolderHistory(view);
            }
   @Override
            protected void onBindViewHolder(@NonNull ViewHolderHistory viewHolderHistory, int i, @NonNull DataViewHistory dataViewHistory) {
                viewHolderHistory.getTxtquantity().setText(String.valueOf(dataViewCart.getQuantity()));

  }

Upvotes: 2

Views: 117

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

You don't have to be sorry, we can undestand what's in your question. Unfortunately, there is no way you can query your actual database and get all the results within all nodes. You cannot solve this without some changes in your database schema. There are two ways in which you can solve this. The first one would be to reduce the number of children by adding that "unknown node", as a property of your product document. Your schema should look like this:

Firestore-root
   |
   --- purchaseHistory
          |
          --- uid
               |
               --- -LmZ- ... A6b9
                     |
                     --- date: "19/08/2019"
                     |
                     --- orderNumber: "20190819_044039"
                     |
                     --- //The other products properties

The second option would to keep the existing structure and to duplicate data. This practice is called denormalization and is a common practice when it comes to Firebase. For a better understanding, i recomand you see this video, Denormalization is normal with the Firebase Database. So create a new node and store there all products. To get them all, simply attach a listener on this new created location and get all products.

Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.

Upvotes: 2

Related Questions