Firebase数据检索错误

时间:2017-11-08 18:10:52

标签: java android firebase firebase-realtime-database

我正在尝试将所有节点的值存储在一个类中,但它似乎没有将它存储在类中。无法弄清楚readDataListener()中的可能更正。正如您所看到的,我正在尝试将所有节点的值存储在一个类中,但不知道我的错误。

SymptomActivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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.ValueEventListener;

public class SymptomActivity extends AppCompatActivity {


private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
Button button;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_symptom);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    button = (Button) findViewById(R.id.button8);
    textView = (TextView) findViewById(R.id.textView2);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);


    // Write a message to the database
    mFirebaseInstance = FirebaseDatabase.getInstance();
    // store app title to 'app_title' node
    mFirebaseInstance.getReference("app_title").setValue("microDoctor");
    mFirebaseDatabase = mFirebaseInstance.getReference("symptomList");
    //mFirebaseDatabase.child("Headache").child("Some Value").setValue("Head");
    //mFirebaseDatabase.setValue("Headache");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            readDataListener();
        }
    });
}

private void readDataListener() {
    mFirebaseDatabase.child("Abdominal Cramps").child("Diarrhea").child("Vomiting").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Symptom symptom = dataSnapshot.getValue(Symptom.class);

            // Check for null
            if (symptom == null) {
                Log.e("Data", "Symptom data is null!");
                return;
            }

            Log.e("Symptom Data", "User data is changed!"+symptom.symptom1);

            // Display newly updated name and email
            textView.setText(symptom.disease);
        }
        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.e("Error", "Failed to read user", error.toException());
        }
    });
}}

Symptom.java 公共阶层症状{

public String symptom1;
public String symptom2;
public String symptom3;
public String disease;

// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Symptom(){

}
public  Symptom(String symptom1, String symptom2, String symptom3, String disease){
    this.symptom1 = symptom1;
    this.symptom2 = symptom2;
    this.symptom3 = symptom3;
    this.disease = disease;
}
}

试图访问的数据

enter image description here

2 个答案:

答案 0 :(得分:1)

您收到该错误是因为您尝试在模型类文件中不存在的节点下获取数据。因此无法映射Vomiting。假设app_title是Firebase root的直接子项,您可以使用String class来获取此值:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("app_title").child("microDoctor").child("symptomList").child("Abdominal Cramps").child("Diarrhea").child("Vomiting");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String disease = ds.child("disease").getValue(String.class);
        Log.d("TAG", disease);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

输出将是:

Food Poisoning

此外,您还需要在模型类中为您的字段添加公共setter和getter,如下所示:

public class Symptom {
    public String disease;

    public Symptom(){}

    public Symptom(String disease){
        this.disease = disease;
    }

    public String getDisease(){
        return disease;
    }
    public void setDisease(String disease){
        this.disease=disease;
    }
}

答案 1 :(得分:0)

public class Symptom {
public String disease;

// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Symptom(){

}
public  Symptom(String disease){
this.disease = disease;
}

 public String getDisease(){
 return disease;
}
  public void setDisease(String disease){
    this.disease=disease;
}
      }