I have been having issues with getting my Firebase data to show up on screen for the app I'm writing. I followed the instructions given in this post, and it all compiles fine, but doesn't actually show anything on screen. I've been stuck on this for a while, and I feel like I'm really close, but not quite there. Help would be much appreciated.
This is my Fridge Fragment:
public class FridgeFragment extends Fragment {
private ArrayList<InventoryItem> items = new ArrayList<>();
private static final String TAG = "FridgeFragement";
private FirebaseRecyclerAdapter<InventoryItem, FridgeHolder> firebaseRecyclerAdapter;
public FridgeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fridge, container, false);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("users");
//Lookup recycler view in activity layout
RecyclerView rvItems = view.findViewById(R.id.fridge_list);
// Set layout manager to position the items
rvItems.setLayoutManager(new LinearLayoutManager(getContext()));
FirebaseRecyclerOptions<InventoryItem> options = new FirebaseRecyclerOptions.Builder<InventoryItem>()
.setQuery(query, InventoryItem.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<InventoryItem, FridgeHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull FridgeHolder holder, int position, @NonNull InventoryItem item) {
holder.setItem(item);
}
@NonNull
@Override
public FridgeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inventory_card, parent, false);
return new FridgeHolder(view);
}
};
// Attach to recycler view
rvItems.setAdapter(firebaseRecyclerAdapter);
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
return view;
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
if (firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
private class FridgeHolder extends RecyclerView.ViewHolder {
private final ImageView image;
private final TextView itemName;
private final TextView itemQuantity;
public FridgeHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.item_photo);
itemName = itemView.findViewById(R.id.item_name);
itemQuantity = itemView.findViewById(R.id.item_quantity);
}
public void setItem(InventoryItem item) {
this.itemName.setText(item.getItemName());
this.itemQuantity.setText(item.getQuantity());
}
public void setItemName(String s) {
itemName.setText(s);
}
public void setQuantity(String s) {
itemQuantity.setText(s);
}
}
}
This is my Inventory Item class:
public class InventoryItem {
private String itemName;
private String type;
private String unit;
private int quantity;
private String location;
public InventoryItem() {
}
public InventoryItem(String name, String foodGroup, int quantity, String unit, String location) {
this.itemName = name;
this.type = foodGroup;
this.quantity = quantity;
this.unit = unit;
this.location = location;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("itemName", itemName);
result.put("type", type);
result.put("unit", unit);
result.put("quantity", quantity);
result.put("location", location);
return result;
}
}
And this is my inventory card .XML layout file:
public class InventoryItem {
private String itemName;
private String type;
private String unit;
private int quantity;
private String location;
public InventoryItem() {
}
public InventoryItem(String name, String foodGroup, int quantity, String unit, String location) {
this.itemName = name;
this.type = foodGroup;
this.quantity = quantity;
this.unit = unit;
this.location = location;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("itemName", itemName);
result.put("type", type);
result.put("unit", unit);
result.put("quantity", quantity);
result.put("location", location);
return result;
}
}
This is the structure of my database.
XML layout of FridgeFragment:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/fridge_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="false"/>
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/fab_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/floating_button"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
fab:fab_labelStyle="@style/menu_labels_style"
fab:fab_labelsPosition="left"
fab:fab_icon="@drawable/ic_plus_white_24dp"
fab:fab_addButtonColorNormal="@color/colorAccent"
fab:fab_addButtonColorPressed="@color/colorAccentPressed">
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addNewItem"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/whitePressed"
fab:fab_title="Add new item"
fab:fab_icon="@drawable/ic_restaurant_black_24dp"
/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</android.support.design.widget.CoordinatorLayout>
Inventory Card XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:paddingBottom="4dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/inventory_card"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_photo"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_name"
android:layout_toEndOf="@+id/item_photo"
android:layout_alignParentTop="true"
android:textSize="30sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_quantity"
android:layout_toEndOf="@+id/item_photo"
android:layout_below="@+id/item_name" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
EDIT 4/2/18
This is the new code for getting the database reference:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = rootRef.child("users").child(uid).child("fridge");