0

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.

enter image description here

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");
Cameron
  • 1,281
  • 1
  • 19
  • 40

1 Answers1

0

Actually I wrote that post and I can say that you haven't followed the instructions as explained there. I'm saying that because I see that you query the database to get users but you are expecting in the RecyclerView to be displayed inventory items, which is not possible, that's why it doesn't actually show anything on screen.

To solve this, you need to change your query like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = rootRef.child("users").child(uid).child("fridge");

Also hope that your .XML file contains the correct RecyclerView as explained there.

Edit:

Remove this keyword from:

this.itemName.setText(item.getItemName());
this.itemQuantity.setText(item.getQuantity())

Comment the following methods from your holder class: public void setItemName(String s) and public void setQuantity(String s).

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for responding! I made those changes, which make sense now that you point them out, but it still isn't showing anything. I added the XML for the FridgeFragment layout if you want to double check that. – Cameron Mar 28 '18 at 13:15
  • Please see my updated answer and tell me if it works. If not, please also share `R.layout.inventory_card` .XML file. – Alex Mamo Mar 28 '18 at 13:23
  • Still not showing up. I added the XML for inventory_card – Cameron Mar 28 '18 at 14:41
  • The item view should only have that RelativeLayout and those 3 nested views. Try this solution, does it work? – Alex Mamo Mar 29 '18 at 05:53
  • Still no. No errors or anything, just nothing on screen. Does it have to do with the fact that FridgeFragment is a Fragment and not an Activity? I was thinking of changing that anyway – Cameron Mar 29 '18 at 17:47
  • No, should work even if is in an fragment. Both have the same life-cycle. Where you are using `holder.setItem(item);`, have you tried to `Log.d("TAG", item.getItemName()); to see if it logs correctly? I think it's a layout problem. – Alex Mamo Mar 29 '18 at 18:17
  • I just tried that, and it's never even hitting that onBindViewHolder method. To make sure, I set a breakpoint there and tried it in debug mode and it never broke, so it's not even getting inside that method – Cameron Mar 30 '18 at 01:00
  • This means that your reference is still pointing to a wrong place. Are you sure is pointing on the right node? – Alex Mamo Mar 31 '18 at 09:21
  • Pretty sure, yeah. What's the easiest way to tell where it's actually pointing, since it isn't where I think it is? – Cameron Apr 01 '18 at 20:08
  • To share again the code where you are instantiating the reference, to take another look. – Alex Mamo Apr 02 '18 at 07:35
  • There must be something else that I cannot see only in this code. Have you tried to comment `AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)`? – Alex Mamo Apr 03 '18 at 15:34
  • Tried that, still nothing. Github repository is linked here, so you can see the full code. https://github.com/jollygreenegiant/WannaCook Thanks again for your efforts – Cameron Apr 09 '18 at 01:54