简体   繁体   中英

retrieve data from firebase to textview returns null how to fix?

I'm trying to retrieve data from firebase to a texview but it returns null, and I was wondering how I can retrieve the respective data based on the uid only of the connected user. I tried in different ways today all afternoon but without any success, someone could help me. I've researched and tried all the stackoverflow and internet solutions as well. I need to retrieve only the "name" field of the structure of the respective connected user. am i doing something wrong? any help is welcome. I need to retrieve only the "name" field of the structure of the respective connected user.

MainActivity Code

public class MainActivity extends AppCompatActivity {
    private FirebaseAuth mAuth;
    Button btn_send;
    EditText et_contact, et_message;
    TextView textView52;
    private FirebaseAuth auth;
    private User usuario = new User();
    private DatabaseReference firebaseDatabase;
    private ValueEventListener valueEventListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth = FirebaseAuth.getInstance();



        btn_send = (Button)findViewById(R.id.btn_send);
        et_contact = (EditText)findViewById(R.id.et_contact);
        et_message = (EditText)findViewById(R.id.et_message);
        textView52 = (TextView)findViewById(R.id.textView52);


        PermissionToConnect();

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = et_contact.getText().toString();
                String message = et_message.getText().toString();

                try{
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(number, null, message, null, null);
                    Toast.makeText(MainActivity.this, "Sent", Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    Toast.makeText(MainActivity.this, "Sending Failed", Toast.LENGTH_SHORT).show();
                }

            }
        });

        DatabaseReference databaseRef= FirebaseDatabase.getInstance()
                .getReference();

        databaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                String data = dataSnapshot.child("Users").child("email").child("name").getValue(String.class);
                textView52.setText(data);
            }

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

            }
        });
    }

- User Class code

User Class

public class User {
    public String name, email, phone;

    public User(){

    }

    public User(String name, String email, String phone) {
        this.name = name;
        this.email = email;
        this.phone = phone;
    }
}

在此处输入图片说明

Change this:


        databaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                String data = dataSnapshot.child("Users").child("email").child("name").getValue(String.class);
                textView52.setText(data);
            }

Into this:


        databaseRef.child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
             for(DataSnapshot ds : dataSnapshot.getChildren()){
                String data = ds.child("name").getValue(String.class);
                textView52.setText(data);
            }
         } 

You need to add Users as a reference and then iterate to get the name or email .


Another way if you are using firebase auth is to retrieve the current user ID thus removing the iteration.

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