从Firebase检索数据时出错

时间:2017-12-21 15:56:10

标签: java android firebase firebase-realtime-database

首先,我是Firebase的新手:)

这是我的代码:

    package com.denizkaanyilmaz.cikmissorular;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.google.firebase.database.ChildEventListener;
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;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Firebase ref = new Firebase("https://cikmissorular-31da2.firebaseio.com/Sinav-01");

        ref.addValueEventListener(new com.firebase.client.ValueEventListener() {
            @Override
            public void onDataChange(com.firebase.client.DataSnapshot snapshot) {
                System.out.println(snapshot.getValue());

                for (com.firebase.client.DataSnapshot postSnapshot: snapshot.getChildren()) {
                    sinav post = postSnapshot.getValue(sinav.class);
                    System.out.println(post.getIsim() + " - " + post.getYil());
                }
            }

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

我在这里尝试做的很简单我只想将数据作为字符串。 这行给了我错误 - > sinav post = postSnapshot.getValue(sinav.class);

这是我的sinav java文件:

package com.denizkaanyilmaz.cikmissorular;

import java.util.List;

/**
 * Created by Denizkaan on 18.12.2017.
 */

public class sinav {

    public String Isim;
    public String Yil;
    public List<Soru> sorular;


    public sinav() {
    }

    public String getIsim() {
        return Isim;
    }

    public String getYil() {
        return Yil;
    }

    public List<Soru> getSorular() {
        return sorular;
    }
}

这是我的火力: Firebase

当我删除名为Soru01和Soru02的孩子或用字符串替换它时我可以检索数据但是当它像这样我无法获取数据。我试图用关于Acticity加载的Sinav-01,Sinav-02,Sinav-03填充我的ListView,但是有问题要获取数据,有人可以帮忙吗? 谢谢

1 个答案:

答案 0 :(得分:1)

要获取this.init = function (modalManager) { _modalManager = modalManager; // ... _modalManager.onClose(function () { abp.event.off('addressCreated', addressCreated); // Turn off this handler }); }; Soru01的名称,请使用以下代码:

Soru02

您的输出将是:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference sorularRef = rootRef.child("Sinav-01").child("Sorular");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.getKey();
            Log.d("TAG", name);
        }
    }

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