Firebase数据检索按节点分类

时间:2017-11-08 20:26:14

标签: 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 class Symptom {

    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

0 个答案:

没有答案