无法在firebase数据库的文本查看器上设置文本

uplii1fm  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(319)

首先,我是个新手。
我正在尝试使用firebase设计一个登录/注册系统。一切都很正常,但每当我试图在textview上显示名称和地址时,它不会显示任何内容,尽管数据库上的所有信息都在更新。
以下是my profile.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
    textViewFullName = (TextView) findViewById(R.id.textViewFullName);
    textViewAddress = (TextView) findViewById(R.id.textViewAddress);
    editTextFullName = (EditText) findViewById(R.id.editTextFullName);
    editTextAddress = (EditText) findViewById(R.id.editTextAddress);
    buttonSaveInfo = (Button) findViewById(R.id.buttonSaveInfo);
    buttonLogOut = (Button) findViewById(R.id.buttonLogOut);

    buttonSaveInfo.setOnClickListener(this);
    buttonLogOut.setOnClickListener(this);

    firebaseAuth = FirebaseAuth.getInstance();
    if (firebaseAuth.getCurrentUser() == null){
        finish();
        startActivity(new Intent(getApplicationContext(), Login.class));
    }

    FirebaseUser user = firebaseAuth.getCurrentUser(); //to use the data of the current user
    textViewUserEmail.setText("Welcome "+user.getEmail());

    databaseReference = FirebaseDatabase.getInstance().getReference();//needed to view or input anything from/to database

////////////////Here is my setText code:////////////////////////////

    if (firebaseAuth.getCurrentUser() != null){
        UserInformation uInfo = new UserInformation();

        //display all the information
        Log.d(TAG, "showData: name: " + uInfo.getName());
        Log.d(TAG, "showData: address: " + uInfo.getAddress());

        textViewFullName.setText(uInfo.getName());
        textViewAddress.setText(uInfo.getAddress());
    }
////////////////////////////////////////////////////
}

@Override
public void onClick(View v) {
    if (v == buttonLogOut){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(this, Login.class));
    }
    if (v == buttonSaveInfo){
        saveUserInformation();
    }
}

private void saveUserInformation(){
    String name = editTextFullName.getText().toString().trim();
    String address = editTextAddress.getText().toString().trim();

    UserInformation userInformation = new UserInformation(name, address);

    //to store data to the individual profiles, I will use the unique id(s) of the logged in firebase user profiles.
    FirebaseUser user = firebaseAuth.getCurrentUser();

    databaseReference.child(user.getUid()).setValue(userInformation);

    Toast.makeText(this, "Profile Updated", Toast.LENGTH_SHORT).show();
}
}

下面是我的userinformation.java:

public class UserInformation {

public String name;
public String address;

public UserInformation(){

}

public UserInformation(String name, String address){
    this.name = name;
    this.address = address;
}

public String getName(){
    return name;
}

public String getAddress(){
    return address;
}
}
ttygqcqt

ttygqcqt1#

你创建了一个新的 UserInformation 对象使用其空构造函数。因此 name 以及 address 是空的,所以当你打电话的时候 getName() 以及 getAddress() 它返回null并输出一个空值。
使用此构造函数初始化 name 以及 address 价值观:

public UserInformation(String name, String address)

相关问题