Firebase数据库无法显示查询的对象列表

时间:2017-10-27 10:22:24

标签: android firebase-realtime-database

由于某种原因,我的Adapter没有获得更新的对象列表,经过一些调试我发现了这一点:当我在Adapter中请求解析后的列表UpdateUI()时,查询不会因某种原因启动并返回一个空列表。也许我在错误的顺序做某事? (只是一些随机文本cus我不能发布问题)

public class ProductListFragment extends Fragment {
    private RecyclerView mRecyclerView;
    private ListFragmentAdapter mAdapter;
    private List<Product> mProducts;

    public ProductListFragment() {
        // Required empty public constructor
    }
    public static ProductListFragment newInstance() {
       return new ProductListFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_list, container, false);
        mRecyclerView = (RecyclerView)view.findViewById(R.id.listRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        updateUI();
    }

    private void updateUI() {
        mProducts = DbOps.get(getActivity()).getProducts();

        if (mAdapter == null) {
            mAdapter = new ListFragmentAdapter(mProducts);
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }


public class DbOps {
    private static DbOps sDbOps;
    private FirebaseDatabase mDb;
    private DatabaseReference dbRef;
    private List<Product> mProducts;
    private Product pr;

    public static DbOps get(Context ctx) {
        if (sDbOps == null) {
            sDbOps = new DbOps(ctx);
        }
        return sDbOps;
    }

    private DbOps(Context ctx) {
        mDb = FirebaseDatabase.getInstance();
        dbRef = mDb.getReference().child("products");
        if(mProducts == null) {
            getProducts();
        }
    }

    public Product getProduct(final String mId) {
        Query query = dbRef.child(mId);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()) {
                    for (DataSnapshot obj: dataSnapshot.getChildren()) {
                        if(obj.getValue(Product.class).getId().equals(mId)) {
                            pr = obj.getValue(Product.class);
                            break;
                        }
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return pr;
    }

    public void addProduct(Product product) {
        dbRef.child(product.getId()).setValue(product);
    }


    public List<Product> getProducts() {
        ValueEventListener listener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot mObject : dataSnapshot.getChildren());
                mProducts = new ArrayList<>();
                Product pr = dataSnapshot.getValue(Product.class);
                mProducts.add(pr);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        dbRef.addValueEventListener(listener);
        return mProducts;
    }

}

1 个答案:

答案 0 :(得分:0)

您不能以您的方式返回Product类的对象,由于onDataChange()方法的异常行为,即使您尝试从中获取数据,也始终为null你的数据库。因此,要解决此问题,您有两个选择:

  1. 您需要在pr方法中使用并声明onDataChange()对象,如下所示:

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()) {
                for (DataSnapshot obj: dataSnapshot.getChildren()) {
                    if(obj.getValue(Product.class).getId().equals(mId)) {
                        Product pr = obj.getValue(Product.class);
                        //do what you want to do with pr
                    }
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
    
  2. 如果您想使用pr以外onDataChange() method对象的值,请从post查看我的回答。