0

首先,我是新手无法在文本查看器上设置Firebase数据库中的文本

我想设计一个使用firebase的登录/注册系统。一切正常,但每当我试图在TextView上显示名称和地址时,尽管所有信息都在数据库上更新,但它没有显示任何内容。

这里是我的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; 
} 
} 
+0

UINFO对象有它里面没有什么,这就是为什么TextView中没有显示任何东西! –

+0

您以最糟糕的方式编写代码。 Firebase.getCurrentUser()是一种静态方法,不需要一次又一次地调用它。 –

+0

谢谢。它的工作现在。关于编码部分,我从几天前开始学习android。所以不那么流利。你能帮我解决任何书名或教程系列吗? – Roy

回答

1

您创建一个使用它的空构造一个新的UserInformation对象。因此它的nameaddress为空,因此当您调用getName()getAddress()时,它将返回null并打印出一个空值。

使用此构造函数初始化nameaddress值:

public UserInformation(String name, String address)