问题描述
我的Firebase数据库结构是:p>
我想检索某个用户的全名,当我有它的密钥的时候。
我的代码:
$ b
public class ProfileActivity扩展了AppCompatActivity的实现View.OnClickListener {
private FirebaseAuth firebaseAuth ;
私人TextView textView;
私人按钮logbutton,wantToDeliverButton,lookForButton;
private String userName;
@Override
protected void onCreate(Bundled savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Firebase.setAndroidContext(this); textView =(TextView)findViewById(R.id.textView2);
Firebase usersRef =新的Firebase(https://myfirstfirebaseauth.firebaseio.com);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()== null){
finish();
startActivity(new Intent(this,LoginActivity.class));
}
FirebaseUser user = firebaseAuth.getCurrentUser();
Firebase ref = usersRef.child(User)。child(user.getUid());
ref.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
userName = dataSnapshot.getValue(User.class).getFullName();
$ b @Override
public void onCancelled(FirebaseError firebaseError){
});
textView.setText(welcome+ userName);
但是它将textView设置为null。我尝试在onDataChange()方法中放置Toast,但不起作用。它看起来甚至没有进入该方法。
$ b
行 FirebaseUser user = firebaseAuth.getCurrentUser();
是正确的,因为如果我写 textView.setText(welcome+ user.getEmail());
- 我真的得到正确的电子邮件。 b
$ b
如何解决这个问题?我使用正确的听众?我不想在数据库中改变任何东西,只是检索。
$ b
编辑:它去onCancelled()。
您正在混合传统2.5.X Firebase SDK的功能(例如:)和新的9.XX SDK(例如:)。这几乎肯定是失败的秘诀。重做你的代码,只使用新的SDK。 。
My Firebase database structure is:enter image description here
I want to retrieve the full name of some user, when i have its key.
My code:
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth firebaseAuth;
private TextView textView;
private Button logbutton,wantToDeliverButton,lookForButton;
private String userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Firebase.setAndroidContext(this);textView = (TextView)findViewById(R.id.textView2);
Firebase usersRef = new Firebase("https://myfirstfirebaseauth.firebaseio.com");
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() == null){
finish();
startActivity(new Intent(this,LoginActivity.class));
}
FirebaseUser user = firebaseAuth.getCurrentUser();
Firebase ref = usersRef.child("User").child(user.getUid());
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.getValue(User.class).getFullName();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
textView.setText("welcome "+userName);
But it set the textView to null. I try to put Toast inside the onDataChange() method, but it doesn't work. It seems it doesn't even get inside the method.
The line FirebaseUser user = firebaseAuth.getCurrentUser();
is correct because if i'm writing textView.setText("welcome "+ user.getEmail());
- i realy get the correct email.
How to solve this? Am i using the correct listener? I don't want to change anything in the database, just retrieve.
Edit: it goes to onCancelled().
You're mixing capabilities from the legacy 2.5.X Firebase SDK (Example: Firebase.setAndroidContext()) and the new 9.X.X SDK (Example: FirebaseAuth.getInstance()). That is almost certainly a recipe for failure. Rework your code to use only the new SDK. The Setup Guide is here.
这篇关于Android - 无法使用给定的uid从Firebase中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!