0

I create a recyclerView and i want to retrieve data from firebase and display it in recyclerview. I have create two models.
Check image for firebase enter image description here

The order which contains id,price,productId,productName and quantity is from an another list.
I want for example setText in orderDetails the productName
I cannot take with the same way because is another list
I take correct address,datetimename and payment method.But when i take productName is null
enter image description here

My Request Model is :

public class Request {

    private String name;
    private String address;
    private String total;
    private String datetime;
    private String paymentMethod;
    private List<CoffeeOrder> order;


    public Request() {
    }

    public Request(String name, String address, String total, String datetime, String paymentMethod, List<CoffeeOrder> order) {
        this.name = name;
        this.address = address;
        this.total = total;
        this.datetime = datetime;
        this.paymentMethod = paymentMethod;
        this.order = order;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public List<CoffeeOrder> getOrder() {
        return order;
    }

    public void setOrder(List<CoffeeOrder> order) {
        this.order = order;
    }
}

My CoffeeOrder Model is:

public class CoffeeOrder {

    private int ID;
    private String ProductId;
    private String ProductName;
    private String Quantity;
    private String Price;

    public CoffeeOrder() {
    }


    public CoffeeOrder(int ID, String productId, String productName, String quantity, String price) {
        this.ID = ID;
        ProductId = productId;
        ProductName = productName;
        Quantity = quantity;
        Price = price;

    }


    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getProductId() {
        return ProductId;
    }

    public void setProductId(String productId) {
        ProductId = productId;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public String getQuantity() {
        return Quantity;
    }

    public void setQuantity(String quantity) {
        Quantity = quantity;
    }

    public String getPrice() {
        return Price;
    }

    public void setPrice(String price) {
        Price = price;
    }
}

My adapter is:

public class UserOrderAdapter extends RecyclerView.Adapter<UserOrderAdapter.UserOrderViewHolder> {

    ArrayList<Request> myList;
    Context context;
    List<CoffeeOrder> coffeeList;


    public UserOrderAdapter(Context context, ArrayList<Request> myList,List<CoffeeOrder> coffeeList) {
        this.myList = myList;
        this.context = context;
        this.coffeeList = coffeeList;
    }

    @NonNull
    @Override
    public UserOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.order_item, parent, false);
        return new UserOrderViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull UserOrderViewHolder holder, int position) {

        Request request = myList.get(position);

        holder.usernameText.setText(request.getName());
        holder.orderTotalPrice.setText(request.getTotal());
        holder.orderDate.setText(request.getDatetime());
        holder.paymentMethod.setText(request.getPaymentMethod());


        for(CoffeeOrder item:coffeeList) {
            holder.orderDetails.setText(item.getProductName());
        }



    }

    @Override
    public int getItemCount() {
        return myList.size();
    }

    public static class UserOrderViewHolder extends RecyclerView.ViewHolder {

        TextView usernameText, orderTotalPrice, orderDate, paymentMethod, orderDetails;


        public UserOrderViewHolder(View itemView) {
            super(itemView);

            usernameText = (itemView).findViewById(R.id.username_text);
            orderTotalPrice = (itemView).findViewById(R.id.order_total_price);
            orderDate = (itemView).findViewById(R.id.order_date);
            paymentMethod = (itemView).findViewById(R.id.payment_method);
            orderDetails = (itemView).findViewById(R.id.order_details);


        }

    }

}

My activity is:

public class UserOrderActivity extends AppCompatActivity {

    RecyclerView recyclerOrders;
    private FirebaseDatabase db = FirebaseDatabase.getInstance();
    private DatabaseReference ref = db.getReference().child("Requests");
    private UserOrderAdapter adapter;
    private ArrayList<Request> list;
    private List<CoffeeOrder> coffeeList;
    Toolbar toolbar;
    TextView userOrderBlankText;

    GifImageView gifImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_order);

        toolbar = findViewById(R.id.toolbar);
        gifImageView = findViewById(R.id.order_giffy);
        userOrderBlankText = findViewById(R.id.user_order_blank_text);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        recyclerOrders = findViewById(R.id.recycler_orders);
        recyclerOrders.setHasFixedSize(true);
        recyclerOrders.setLayoutManager(new LinearLayoutManager(this));

        list = new ArrayList<>();
        coffeeList = new ArrayList<>();
        adapter = new UserOrderAdapter(UserOrderActivity.this, list,coffeeList);
        recyclerOrders.setAdapter(adapter);


        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                    Request request = dataSnapshot.getValue(Request.class);
                    list.add(request);
                }

                adapter.notifyDataSetChanged();

                if (list.size() == 0) {
                    gifImageView.setVisibility(GifImageView.VISIBLE);
                    userOrderBlankText.setVisibility(View.VISIBLE);
                }
            }

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

            }
        });

    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
usern
  • 9
  • 4
  • You are assigning your list to the adapter while it is still empty – kgori_dev Feb 23 '21 at 10:29
  • 1
    @kgori_dev how can i work? – usern Feb 23 '21 at 10:30
  • Check answers section, i answered – kgori_dev Feb 23 '21 at 10:33
  • @kgori_dev And how can get the product name for example in adapter? – usern Feb 23 '21 at 10:46
  • What exactly in this code doesn't work the way you expect? Please respond with @AlexMamo – Alex Mamo Feb 23 '21 at 11:07
  • @AlexMamo I take correct address,datetimename and payment method.But when i take productName is null. I update my code. If you want chack code adapter – usern Feb 23 '21 at 11:08
  • The problem is that your "ProductName" in the class doesn't match the one in the database, which is "productName". See lower-case "p"? Both must match. Please check the duplicate to see how you can solve this. – Alex Mamo Feb 23 '21 at 11:18
  • @AlexMamo man check my two models. productName etc is a list which contain in another model – usern Feb 23 '21 at 11:24
  • Yes, and that model has a property called `ProductName`, which is different than the one in the database. Check the duplicate. – Alex Mamo Feb 23 '21 at 11:27
  • @AlexMamo pff how can solve it? – usern Feb 23 '21 at 11:30
  • Exactly as explained in the duplicate answer. – Alex Mamo Feb 23 '21 at 11:37
  • @AlexMamo If you have public getters and setters, the *field* name can be anything you want. The property in the JSON is `productName`, which matches with the getter and setter: `public String getProductName() ` and `public void setProductName(String productName) {` as far as I can see. – Frank van Puffelen Feb 23 '21 at 15:02
  • @FrankvanPuffelen Yes, you're right. Thanks for opening the question. – Alex Mamo Feb 23 '21 at 17:20
  • @usern What does `request.getProductName()` return? – Alex Mamo Feb 23 '21 at 17:20
  • @AlexMamo hi man and thanks for anwser. I cannot take request.getProductName(). Because product name is property on Model CoffeeOrder. In model Request i take the Model CoffeeOrder as list – usern Feb 24 '21 at 07:55
  • @AlexMamo with request can take all fields from Model Request..such as request.getName(),request.getTotal(),request.getDatetime(),request.getPaymentMethod() and request.getOrder which is list coffeeorder..But this getOrder i donw know how can edit it. – usern Feb 24 '21 at 07:57

1 Answers1

0

Try this

public class UserOrderActivity extends AppCompatActivity {

RecyclerView recyclerOrders;
private FirebaseDatabase db = FirebaseDatabase.getInstance();
private DatabaseReference ref = db.getReference().child("Requests");
private UserOrderAdapter adapter;
private ArrayList<Request> list;

Toolbar toolbar;
TextView userOrderBlankText;

GifImageView gifImageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_order);

    toolbar = findViewById(R.id.toolbar);
    gifImageView = findViewById(R.id.order_giffy);
    userOrderBlankText = findViewById(R.id.user_order_blank_text);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    recyclerOrders = findViewById(R.id.recycler_orders);
    recyclerOrders.setHasFixedSize(true);
    recyclerOrders.setLayoutManager(new LinearLayoutManager(this));

    list = new ArrayList<>();
    

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                Request request = dataSnapshot.getValue(Request.class);
                list.add(request);
            }
            adapter = new UserOrderAdapter(UserOrderActivity.this, list);
            recyclerOrders.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            if (list.size() == 0) {
                gifImageView.setVisibility(GifImageView.VISIBLE);
                userOrderBlankText.setVisibility(View.VISIBLE);
            }
        }

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

        }
    });

}
}
kgori_dev
  • 154
  • 2
  • 18
  • 1
    And how can get the product name for example in adapter? – usern Feb 23 '21 at 10:37
  • 1
    @AlexMamo I take correct address,datetimename and payment method.But when i take productName is null. I update my code. If you want chack code adapter – usern Feb 23 '21 at 11:09
  • @AlexMamo thank you for the note...ill edit the answer and add relevant information – kgori_dev Feb 23 '21 at 11:45
  • @usern use the `this` key word on your constructor and setters for example `public void setProductName(String productName) { this.ProductName = productName; }` – kgori_dev Feb 23 '21 at 11:47
  • @kgori_dev why in set? – usern Feb 23 '21 at 12:03
  • @usern please check [this link](https://www.w3schools.com/java/ref_keyword_this.asp#:~:text=The%20this%20keyword%20refers%20to,a%20method%20or%20constructor%20parameter) out for more information about the use of `this` keyword. Basically is used for referencing or referring to the current class instance variables – kgori_dev Feb 23 '21 at 12:08