首先,我是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;
    }
}


这是我的基地:
java - 从Firebase检索数据时出错-LMLPHP

当我删除孩子的名为Soru01和Soru02或将它们替换为字符串时,我可以检索数据,但是这样的话,我将无法获取数据。我尝试在Acticity负载上用Sinav-01,Sinav-02,Sinav-03填充ListView,但是在获取数据时遇到问题,有人可以帮忙吗?
谢谢

最佳答案

要获取Soru01Soru02的名称,请使用以下代码:

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);


您的输出将是:

Soru01
Soru02

07-28 04:04