模型类返回null

时间:2019-02-24 15:53:36

标签: java firebase firebase-realtime-database

[模型类返回null,数据来自Firebase数据库,但在获取imageUrl后将其赋予null。

实际上,我正在尝试从以前保存的数据库中获取图像URL。

    firebaseDatabase=FirebaseDatabase.getInstance();   

    databaseReference=firebaseDatabase.getInstance().
    getReference("Catagories");
    mupload=new ArrayList<>();`


     databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot post : dataSnapshot.getChildren())
        {
            Upload upload=post.getValue(Upload.class);
            mupload.add(upload);
                        }
            customQuoteAdapter=new CustomQuoteAdapter( Images.this,     

          mupload);
            recyclerView.setAdapter(customQuoteAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(getApplicationContext(),   
  "ERROR"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

2 个答案:

答案 0 :(得分:0)

应该吗?

public String getImageUrl() {
    return this.imageUrl;
}

答案 1 :(得分:0)

您的JSON中有一个bussiness(原文如此)级别,您没有在代码中处理。解决此问题的最简单方法是将侦听器附加到该bussiness节点:

databaseReference=firebaseDatabase.getInstance().getReference("Catagories/Bussiness");
mupload=new ArrayList<>();`


 databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot post : dataSnapshot.getChildren()) {
            Upload upload=post.getValue(Upload.class);
            mupload.add(upload);
        }
        customQuoteAdapter=new CustomQuoteAdapter( Images.this, mupload);
        recyclerView.setAdapter(customQuoteAdapter);
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Toast.makeText(getApplicationContext(), "ERROR"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();

    }
});

如果要获取Categories下所有节点的所有URL,则需要在onDataChange内添加一个循环。像这样:

databaseReference=firebaseDatabase.getInstance().getReference("Catagories");
mupload=new ArrayList<>();`


 databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot categorySnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot linkSnapshot : categorySnapshot.getChildren()) {
                Upload upload=linkSnapshot.getValue(Upload.class);
                mupload.add(upload);
            }
        }
        customQuoteAdapter=new CustomQuoteAdapter( Images.this, mupload);
        recyclerView.setAdapter(customQuoteAdapter);
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Toast.makeText(getApplicationContext(), "ERROR"+databaseError.getMessage(), Toast.LENGTH_SHORT).show();

    }
});