Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – retrieve data of specific value in Firebase recycle view in android studio

Posted on November 14, 2022

Solution 1 :

Try it this way.
Assuming that AdminOrders.class is your model class.

{
    FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder> adapter;

    FirebaseRecyclerOptions<AdminOrders> options =
                    new FirebaseRecyclerOptions.Builder<AdminOrders>()
            .setQuery(shippedOrdersRef.orderByChild("state").equalTo("shipped"), AdminOrders.class)
            .build();

    adapter = new FirebaseRecyclerAdapter<Products_Model, OrderProductsFragment.AdminOrderViewHolder>(options){
          @SuppressLint("SetTextI18n")
                @Override
                protected void onBindViewHolder(@NonNull final AdminNewOrdersActivity.AdminOrderViewHolder holder, final int position, @NonNull final AdminOrders model) {
        // Set your elements here


                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view, parent, false);
                return new AdminOrderViewHolder(view);
                }
    @NonNull
                @Override
                public AdminOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          //Set your view here
        }
     };
            recyclerView.setAdapter(adapter);
            adapter.startListening();
    }

Sorry for the alignments and the brackets if I have mistaken.

Solution 2 :

One way is like this (you have an if-statement and you store the ones you want in an array):

List<String> shipped = new ArrayList<>();

shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child("Orders");
shippedOrdersRef.addValueEventListener(new ValueEventListener()
   @Override
   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
       for(DataSnapshot ds : dataSnapshot.getChildren()){
           Orders order = ds.getValue(Orders.class);
           if(order.getState().equals("shipped")) shipped.add(order.getState());
       }
    });

Where Orders is the name of the class with the constructor of the orders.

Another way, faster, is this:

shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child("Orders");
shippedOrdersRef.orderByChild("state").equalTo("shipped").addValueEventListener(new ValueEventListener()
   @Override
   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
      //your wanted value is firstChild.getKey()
   });

Here is a helpful link.

Hope I helped!

Problem :

i have an e-commerce android app and i have all orders(state = shipped & not shipenter image description hereped) which are received by admin in recycler view from Firebase realtime database. Now, i want to get shipped orders(state = shipped) in another activity and i dont know how to retrieve only those orders which have state = shipped. I have tried several things but cant get around this. So anyone could help please answer.
My recyclerView code looks this this:

shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child("Orders")    
FirebaseRecyclerOptions<AdminOrders> options = new FirebaseRecyclerOptions.Builder<AdminOrders>()
            .setQuery(shippedOrdersRef, AdminOrders.class).build();

    FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder> adapter =
            new FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull AdminNewOrdersActivity.AdminOrderViewHolder holder, final int i, @NonNull AdminOrders adminOrders)
                {
                    holder.userName.setText("Name: " + adminOrders.getName());
                    holder.userPhoneNumber.setText("Phone: " + adminOrders.getPhone());
                    holder.userTotalPrice.setText("Total Amount =  Rs." + adminOrders.getTotalAmount());
                    holder.userDateTime.setText("Order at: " + adminOrders.getDate() + "  " + adminOrders.getTime());
                    holder.userShippingAddress.setText("Shipping Address: " + adminOrders.getAddress() + ", " + adminOrders.getCity());
                    holder.orderState.setText("" + adminOrders.getState());

     }

                @NonNull
                @Override
                public AdminNewOrdersActivity.AdminOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
                {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_layout, parent, false);
                    return new AdminNewOrdersActivity.AdminOrderViewHolder(view);
                }
            };
    shippedOrdersList.setAdapter(adapter);
    adapter.startListening();
}

    [My database looks like this][1]

Comments

Comment posted by i.stack.imgur.com/qhac2.png

i.stack.imgur.com/qhac2.png

READ  [ANSWERED] android - Camera 2 manually checking if ISO and SHUTTER_SPEED are supported
Powered by Inline Related Posts

Comment posted by Muhammad Hamza Munir

this is picture of my database

Comment posted by Muhammad Hamza Munir

i didn’t understand how to use this query and where.

Comment posted by KalanaChinthaka

I am sorry. I updated the answer. Please change the values if I have missed some since this is something I used for my current projects.

Comment posted by Muhammad Hamza Munir

i want to do it using second way so where do i add this code inside recyclerview adapter or where?

Comment posted by LoukasPap

It depends on which screen you want to show them up. You say you want the shipped orders in another activity, so put it in your other class(activity). Is the code above, the activity in which you want to take the shipped states?

Comment posted by LoukasPap

Add ‘orderByChild(“state”).equalTo(“shipped”)’ next to ‘shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child(“Orders”)’

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress