简体   繁体   English

如何获取保存在 USER_ID 中的数据?

[英]How to get the data saved in a USER_ID?

The data is saved correctly, as you can see in the following image:数据已正确保存,如下图所示:

在此处输入图片说明

I use this code:我使用这个代码:

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

And to obtain the data I use the following code: mDatabase.child("Usuario").child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) {为了获取数据,我使用以下代码: 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) {

        }
    });

But it does not show the data and when I test it on the device the app is closed.但它不显示数据,当我在设备上测试它时,应用程序已关闭。 What would be the error?会是什么错误?

If you want all data then create model class and then stored datasnapshot into that model class like this如果您想要所有数据,则创建模型类,然后像这样将数据快照存储到该模型类中

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

Or you want single data then just get the child like this或者你想要单个数据然后像这样得到孩子

String idJuego = dataSnapshot.child("childname").getValue().toString; String idJuego = dataSnapshot.child("childname").getValue().toString;

And then set that string into textView.然后将该字符串设置为 textView。

try to convert from Integer to string...尝试从整数转换为字符串...

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

Because you are saving the uid as the key of your user object and in the same time as a property within the user object, to set that id to your idffTextView , please use either the following lines of code:因为您将uid保存为用户对象的键,同时保存为用户对象中的一个属性,要将该 id 设置为您的idffTextView ,请使用以下代码行:

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

Or:或者:

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

Please note, that in both cases the id is of type String and not int .请注意,在这两种情况下, id都是String不是int类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM