【问题标题】:How to get data from second generated key in Firebase?如何从 Firebase 中的第二个生成的密钥获取数据?
【发布时间】:2021-12-30 15:30:25
【问题描述】:

我希望能够获取标记为黄色的数据,然后显示在Android的recyclerView中,但是我不知道如何获取用标记的push()随机生成的Key红色。

我看过各种视频、教程和文档,但我最大的收获是将数据显示为绿色。

我附上了我用绿色显示的代码(我希望能够以黄色显示数据)

public class mostrarActivity extends AppCompatActivity {

RecyclerView recyclerPa;
DatabaseReference database;
Adapter adapter;
ArrayList<Pacientes> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mostrar);

    recyclerPa = findViewById(R.id.RecyclerPa);
    database = FirebaseDatabase.getInstance().getReference("Usuarios");
    recyclerPa.setHasFixedSize(true);
    recyclerPa.setLayoutManager(new LinearLayoutManager(this));

    list = new ArrayList<>();
    adapter = new Adapter(this,list);
    recyclerPa.setAdapter(adapter);

    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                Pacientes pacientes = dataSnapshot.getValue(Pacientes.class);
                list.add(pacientes);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    由于您要显示的数据嵌套在两级动态键下,因此您需要在getChildren() 上进行两个嵌套循环。

    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot userSnapshot : snapshot.getChildren()){
                for (DataSnapshot patientSnapshot : userSnapshot.getChildren()){ // ? add an extra loop
                    if (patientSnapshot.hasChild("nombre")) { // ? check for object
                        Pacientes pacientes = patientSnapshot.getValue(Pacientes.class);
                        if (pacientes != null) { // ? skip non-patient nodes (see note below)
                            list.add(pacientes);
                        }
                    }
                }
            }
            adapter.notifyDataSetChanged();
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            throw error.toException(); // ? Never ignore errors
        }
    });
    

    一般而言,您的数据结构违反了 Firebase 对 not building nested data 的建议,因此我建议您研究 structuring data 上的文档并在 Firebase for SQL developers 上观看 David 的优秀视频系列。

    【讨论】:

    • 感谢您的回答,我曾尝试使用另一个 for,但与您放置的一样,它给了我同样的错误“PID:2719 com.google.firebase.database.DatabaseException:Can' t 将 java.lang.String 类型的对象转换为 com.example.psicoapp.Pacientes 类型。再次感谢所有的解释,它真的对我帮助很大,我已经想了解 Firebase,我会看到你给我的所有信息和视频,但是单独查询,使用 Firestore 更好吗?然后我打算在“Pacientes”中托管用户从应用上传的一些照片网址
    • 我又添加了一项检查,看看我们是否可以防止该错误,顺便说一句,这也来自我在答案末尾解释的数据结构问题。 Firestore 和实时数据库都有各自的用例。为了帮助您做出决定,请查看firebase.google.com/docs/database/rtdb-vs-firestore
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2016-04-19
    • 2020-11-23
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多