gpt4 book ai didi

java - 从 Firebase 检索数据时出错

转载 作者:行者123 更新时间:2023-12-02 11:50:14 25 4
gpt4 key购买 nike

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

当我删除 child 的 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

关于java - 从 Firebase 检索数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com