从Firebase检索数据无法正常工作

时间:2019-04-02 23:29:13

标签: android firebase-realtime-database

我目前是Android和Firebase开发的新手。问题是我可以将数据发送到实时数据库,但是当我想将信息检索回应用程序时,什么也没发生。编译时没有任何错误。如果有人可以提供建议,则非常感谢

public class MainActivity extends AppCompatActivity {

    Button edButton;
    TextView Name;
    TextView Email;
    TextView Age;
    TextView AboutMe;
    Button reButton;

    FirebaseDatabase mDatabase;

    DatabaseReference mDbRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDbRef = FirebaseDatabase.getInstance().getReference().child("Profile/User");

        Name = findViewById(R.id.NameTextView);
        Age = findViewById(R.id.AgeTextView);
        Email = findViewById(R.id.EmailTextView);
        AboutMe = findViewById(R.id.AboutMeTextView);
        edButton = findViewById(R.id.editButton);
        reButton = findViewById(R.id.reButton);


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

                Intent intent = new Intent(MainActivity.this, EditProfile.class);
                startActivity(intent);
            }
        });

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

                mDbRef = FirebaseDatabase.getInstance().getReference().child("Profile/User");

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

                        String name = (String) dataSnapshot.child("Name").getValue();
                        String age = (String) dataSnapshot.child("Age").getValue();
                        String email = (String) dataSnapshot.child("Email").getValue();
                        String aboutMe = (String) dataSnapshot.child("AboutMe").getValue();

                        Name.setText(name);
                        Age.setText(age);
                        Email.setText(email);
                        AboutMe.setText(aboutMe);
                    }

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

                    }
                });


            }
        });


    }
}

enter image description here enter image description here

0 个答案:

没有答案