简体   繁体   中英

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

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

Or you want single data then just get the child like this

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

And then set that string into textView.

try to convert from Integer to string...

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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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