FirebaseRecyclerAdapter不在屏幕上显示数据

时间:2018-03-28 03:09:45

标签: java android firebase firebase-realtime-database

我一直遇到让我的Firebase数据显示在我正在编写的应用程序的屏幕上的问题。我按照 this 帖子中给出的说明进行了操作,这一切都很好,但实际上并没有在屏幕上显示任何内容。我已经坚持了一段时间,我觉得我真的很亲密,但不是那里。非常感谢帮助。

这是我的冰箱片段:

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);
    }
}

}

这是我的库存物品类:

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;
}

}

这是我的库存卡.XML布局文件:

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;
}

}

这是我数据库的结构。

enter image description here

FridgeFragment的XML布局:

<?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>

库存卡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>

编辑4/2/18

这是获取数据库引用的新代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

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

1 个答案:

答案 0 :(得分:0)

其实我写了那篇帖子,我可以说你没有按照那里的说明进行操作。我这样说是因为我看到你查询数据库以获取用户,但你期望在RecyclerView显示库存物品,这是不可能的,这就是为什么它实际上并没有在屏幕上显示任何内容。

要解决此问题,您需要更改您的查询:

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

同时希望您的.XML文件包含正确的RecyclerView,如此处所述。

修改

从以下位置删除this关键字:

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

评论您的持有人类中的以下方法:public void setItemName(String s)public void setQuantity(String s)