从Firebase数据库Android中检索空值

从Firebase数据库Android中检索空值

本文介绍了从Firebase数据库Android中检索空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按Firebase数据库中的键进行搜索,但是它检索的是null,我不知道为什么.

I'm searching by key in my Firebase database, but it retrieves null and I don't know why.

data.getUrl()data.getText()都给我空值.

在调试中,在行之前

test data = dataSnapshot.getValue(test.class);

它告诉我数据应该已经检索了我要查找的内容,但是当该行执行时,它为我提供了空值.

it shows me that data should've retrieved what I'm looking for, but when that line executes, it gives me null.

数据库结构:

{
  "images" : {
    "partner " : {
      "text" : "partner",
      "url" : "http://res.cloudinary.com/dg3jylcsw/image/upload/v1483565650/sister_x3lv6j.png"
    },
    "sister" : {
      "text " : "sister",
      "url" : "http://res.cloudinary.com/dg3jylcsw/image/upload/v1483565650/sister_x3lv6j.png"
    }
  }
}

测试课程:

package com.example.android.testy;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class test {
    private String text;
    private String url;

    public test() {
        // empty default constructor, necessary for Firebase to be able to deserialize blog posts
    }
    public String getText() {
        return text;
    }

    public String getUrl() {
        return url;
    }
}

主要活动:

package com.example.android.testy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);

        Firebase rootRef = new Firebase("https://test-219f8.firebaseio.com/images");
        Query queryRef = rootRef.orderByKey().equalTo("sister");
        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                    test data = dataSnapshot.getValue(test.class);

                    System.out.println(data.getUrl());

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });

    }


}

推荐答案

您需要使用addChildEventListener而不是addValueEventListener.

尝试:

queryRef.addChildEventListener(new ChildEventListener() {
    public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
        test data = dataSnapshot.getValue(test.class);

                    System.out.println(data.getUrl());
    }
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    }
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    }
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    }
    public void onCancelled(FirebaseError firebaseError) { }
});

这篇关于从Firebase数据库Android中检索空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:18