如何将数据保存在USER_ID中?

问题描述 投票:-1回答:3

正确保存数据,如下图所示:

enter image description here

我用这个代码:

btnDatos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String name = nameTextView.getText().toString();
            String email = emailTextView.getText().toString();
            String id = idTextView.getText().toString();
            String idJuego = idffTextView.getText().toString();


            Map hopperUdates = new HashMap();
            hopperUdates.put("name", name);
            hopperUdates.put("email", email);
            hopperUdates.put("id", id);
            hopperUdates.put("idFreeFire", idJuego);

            mDatabase.child("Usuario").child(mAuth.getCurrentUser().getUid()).updateChildren(hopperUdates);
    }
});

为了获取数据,我使用以下代码:mDatabase.child(“Usuario”)。child(mAuth.getCurrentUser()。getUid())。addValueEventListener(new ValueEventListener(){@Override public void onDataChange(@NonNull DataSnapshot dataSnapshot ){

            int idJuego = dataSnapshot.getValue(Integer.class);
            idffTextView.setText(idJuego);


        }

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

        }
    });

但它没有显示数据,当我在设备上测试时,应用程序已关闭。会出现什么错误?

java android firebase firebase-realtime-database
3个回答
0
投票

如果您想要所有数据,则创建模型类,然后将数据快照存储到该模型类中

Model model = dataSnapshot.getValue(Model.class);

或者你想要单个数据,然后就像这样得到孩子

String idJuego = dataSnapshot.child(“childname”)。getValue()。toString;

然后将该字符串设置为textView。


0
投票

尝试从整数转换为字符串...

int idJuego = dataSnapshot.getValue(Integer.class); idffTextView.setText(String.valueOf(idJuego ));


0
投票

因为您将uid保存为用户对象的键,并且与用户对象中的属性同时保存,要将该id设置为idffTextView,请使用以下代码行:

String idJuego = dataSnapshot.getkey();
idffTextView.setText(idJuego);

要么:

String idJuego = dataSnapshot.child("id").getValue(String.class)
idffTextView.setText(idJuego);

请注意,在两种情况下,id都是String类型,而不是int

© www.soinside.com 2019 - 2024. All rights reserved.