Reputation: 11
This is the Firebase Database structure, where we are trying to access child values from Retailer>Amritha Stores>r_history>30-06-2022>items>0>Khara Mixture.
This is the screenshot of the application's recycler view where we want to display data from Retailer>Amritha Stores>r_history>30-06-2022>items>0>Khara Mixture>, but it's displaying null.
Just to give clarity to our intention in the data model
This is the part of our Fragment.java where we populate RecyclerView.
FirebaseRecyclerOptions<retailer_model_datewise_detailsdisp> options =
new FirebaseRecyclerOptions.Builder<retailer_model_datewise_detailsdisp>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Retailer").child(username).child("r_history").child(datenew).child("Items"),retailer_model_datewise_detailsdisp.class)
.build();
adapter=new adapter_retailerside_datewise_dispoforder(options);
adapter.startListening();
recyclerView.setAdapter(adapter);
AdapterClass (adapter_retailerside_datewise_dispoforder):
public class adapter_retailerside_datewise_dispoforder extends FirebaseRecyclerAdapter<retailer_model_datewise_detailsdisp,adapter_retailerside_datewise_dispoforder.myviewholder> {
public adapter_retailerside_datewise_dispoforder(@NonNull FirebaseRecyclerOptions<retailer_model_datewise_detailsdisp> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull retailer_model_datewise_detailsdisp model) {
holder.name.setText("Name: "+model.getName());
holder.price.setText("Price: "+model.getPrice());
holder.quan.setText("Quan: "+model.getQuan());
holder.weight.setText("Weight: "+model.getWeight());
holder.totprice.setText(model.getTotal());
}
@NonNull
@Override
public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow_design_retailerside_datewise_detailsdisp,parent,false);
return new myviewholder(view);
}
public class myviewholder extends RecyclerView.ViewHolder {
TextView name,price,quan,weight,totprice;
public myviewholder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.datewisename);
price=itemView.findViewById(R.id.datewiseprice);
quan=itemView.findViewById(R.id.datewisequan);
weight=itemView.findViewById(R.id.datewiseweight);
totprice=itemView.findViewById(R.id.totalpricehere);
}
}
}
Model Class (retailer_model_datewise_detailsdisp):
public class retailer_model_datewise_detailsdisp {
String name,price,quan,weight,total;
public retailer_model_datewise_detailsdisp() {
}
public retailer_model_datewise_detailsdisp(String name, String price, String quan, String weight,String total) {
this.name = name;
this.price = price;
this.quan = quan;
this.weight = weight;
this.total=total;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuan() {
return quan;
}
public void setQuan(String quan) {
this.quan = quan;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
Upvotes: 1
Views: 79
Reputation: 598718
When deserializing the JSON from the database into a Java object, the FirebaseUI adapter takes the direct child nodes of the path that you load data from and tries to load the Java class that you told it to from that.
So in your code, it tries to populate the properties/fields of the retailer_model_datewise_detailsdisp
objects from the data at /Retailer/$username/r_history/$datenew/Items
. Under that path it finds a single key 0
, so it tries to create a retailer_model_datewise_detailsdisp
from that. Then it finds a single property Khara Mixture
, so it looks for a field with that name on the retailer_model_datewise_detailsdisp
object, and doesn't find it.
I recommend removing the Khara Mixture
level from your JSON structure, as you already have that value in the name
property too. Once you update the data model to have the name
, date
, and other properties directly under 0
, the adapter will be able to load the object from it.
Upvotes: 1