-1

https://ibb.co/ieRwsJ

https://ibb.co/c4xmsJ

https://ibb.co/d8gJ6d

this is my database structure.

order
  key
    address
    cart node with 3 childs
    phone
    total etc

I have tried all the ways to get into the cart object that is created but when it gives me an error and when i try to retrieve it in a recyclerview it just returns the first "0" child nothing else. I have tried using hashmaps and arraylists and i have also given the recyclerview adapter to show the retrieval of data.

//this is my pojo below.

public class Order {

private String phone;
private String address;
private String time;
private String date;
private String message;
private String total;
private HashMap<String,Cart> cart;
private String tax;
private String status;

public Order() {
}

//getter and setter


//code for looping over the child node
  orderDB.child("orders").child(currentUser).addValueEventListener(new 
  ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot orderSnapshot : dataSnapshot.getChildren()) {
                    Order order = orderSnapshot.getValue(Order.class);
                    onGoingOrderList.add(order);                   
                    rvOnGoingOrders.setAdapter(onGoingOrderadapter);

                }

            }
        }

//code for my adapter
public void onBindViewHolder(OngoingOrderViewholder holder, int position) {
   holder.tvProductnameOngoingOrders.setText(onGoingOrderArrayList.get(position).getCart().get(position).getName());

this is the error that i am getting if i retrieve the data into a hashmap https://ibb.co/d8gJ6d

  • 2
    Please post your datastructure/code as text instead of images/links. – André Kool Jul 04 '18 at 08:39
  • changed it to text. – indrajit saha Jul 04 '18 at 09:40
  • "it gives me an error and when i try to retrieve it " What error? – Frank van Puffelen Jul 04 '18 at 14:34
  • Also note that you'll want to call `onGoingOrderList.notifyDataSetChanged()` at the end of `onDataChange`. – Frank van Puffelen Jul 04 '18 at 14:35
  • i think the best way to get the data from firebase is to create an arraylist inside the for loop and add all the data through the pojo class. Then retrieve it in the recyclerview adapter using the arraylist.get(position).getProperty. I have populated many recyclerviews but this time it does not seem to work at all. I have edited my code and put the setAdapter inside the for loop which is the norm of retrieving the data and have done it a number of times but with an arraylist as a variable in POJO it doesn't seem to work. – indrajit saha Jul 04 '18 at 16:17

1 Answers1

1

The problem in your code is that you are setting the adapter inside the for loop. To solve this, move the following line of code:

rvOnGoingOrders.setAdapter(onGoingOrderadapter);

Out of the for loop, otherwise you'll end up having only one item displayed.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • it didn't work unfortunately still shows only 1 item. Edited my question with your logic. – indrajit saha Jul 04 '18 at 09:07
  • If you are trying to use `Log.d("TAG", onGoingOrderList.toString());` after this line of code `rvOnGoingOrders.setAdapter(onGoingOrderadapter);` what does it print? Does the list have item or is it empty? – Alex Mamo Jul 04 '18 at 09:27
  • no it isn't empty as i am retrieving one item the first one from the list https://ibb.co/jmy4wd – indrajit saha Jul 04 '18 at 09:38
  • Please take a look [here](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview) to see what to set the adapter. – Alex Mamo Jul 04 '18 at 09:46
  • i think you should see my data structure once .You could suggest a better way of structuring data for the orders node if possible. – indrajit saha Jul 04 '18 at 16:12
  • Have you tryed to call `onGoingOrderList.notifyDataSetChanged()` at the end of `onDataChange()`? – Alex Mamo Jul 05 '18 at 08:10