FirebaseRecyclerAdapter没有调用onBindViewHolder而不显示数据

时间:2018-04-14 23:18:21

标签: android firebase firebase-realtime-database firebaseui

我正在尝试使用FirebaseRecyclerAdapter在RecyclerView中显示屏幕上的数据,但没有数据显示。我已经使用调试模式来确定在将新项添加到列表时未调用onBindViewHolder,但是它正在正确地添加到数据库中。我已经查看了Stack Overflow,并且所发布的解决方案都没有帮助。

这是我的数据库的结构:

This is the structure of my database

这是我的片段代码:

    package com.jggdevelopment.wannacook.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.jggdevelopment.wannacook.AddItemDialogFragment;
import com.jggdevelopment.wannacook.InventoryAdapter;
import com.jggdevelopment.wannacook.InventoryItem;
import com.jggdevelopment.wannacook.MyViewHolder;
import com.jggdevelopment.wannacook.R;

import java.util.ArrayList;

/**
 * Handles actions taken inside the "Fridge" tab of the app.
 * Created by Cameron on 1/18/2018.
 */

public class FridgeFragment extends Fragment {

    private ArrayList<InventoryItem> items = new ArrayList<>();
    private static final String TAG = "FridgeFragement";
    private FirebaseRecyclerAdapter<InventoryItem, FridgeHolder> firebaseRecyclerAdapter;
    private RecyclerView rvItems;

    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("/users");

        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

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

        //Lookup recycler view in activity layout
        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) {
            @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);
            }

            @Override
            protected void onBindViewHolder(@NonNull FridgeHolder holder, int position, @NonNull InventoryItem item) {
                super.onBindViewHolder(holder, position);
                holder.setItem(item);
                Log.d("TAG", item.getItemName());
            }


        };


        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
        rvItems.setAdapter(firebaseRecyclerAdapter);
    }

    @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) {
            itemName.setText(item.getItemName());
            itemQuantity.setText(item.getQuantity());
        }
    }
}

可以在Github上找到完整的代码:https://github.com/jollygreenegiant/WannaCook

2 个答案:

答案 0 :(得分:0)

您确定数据库安全规则是否正确,并且已登录的用户是否有权读取该路径?如果您确实遇到权限问题,则应在日志中看到float offset = 0; void setup() { size(512,512); noStroke(); fill(255); } void draw() { background(0); offset+= .01; int resolution = 360; int radius = 100; int noiseHeight = 50; int circleX = width/2; int circleY = height/2; beginShape(); for(int i=0; i<resolution; i++) { float angle = map(i,0,resolution,0,TWO_PI); float circleOffX = cos(angle+offset) * (radius+noise(i*.1,offset)*noiseHeight); float circleOffY = sin(angle+offset) * (radius+noise(i*.1,offset)*noiseHeight); float x = circleX + circleOffX; float y = circleY + circleOffY; vertex(x,y); } endShape(); }

答案 1 :(得分:0)

在根查询中,您有/users。删除/