简体   繁体   English

从 firebase 检索数据到 textview 返回 null 如何修复?

[英]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.我正在尝试将数据从 firebase 检索到 texview,但它返回 null,我想知道如何仅根据连接用户的 uid 检索相应的数据。 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.我也研究并尝试了所有的 stackoverflow 和 Internet 解决方案。 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 .您需要添加Users作为参考,然后迭代以获取nameemail


Another way if you are using firebase auth is to retrieve the current user ID thus removing the iteration.如果您使用 firebase 身份验证,另一种方法是检索当前用户 ID,从而删除迭代。

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

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