问题描述
我的Firebase 数据库
结构是:
当我拥有某个用户的密钥时,我想检索其全名.
I want to retrieve the full name of some user, when I have its key.
我的代码:
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);
}
}
但是它将textView
设置为null
.我尝试将 Toast 放在 onDataChange()
方法中,但它不起作用.似乎它甚至没有进入方法.
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.
行 FirebaseUser user = firebaseAuth.getCurrentUser();
是正确的,因为如果我正在写 textView.setText("welcome "+ user.getEmail());
- 我真的收到了正确的电子邮件.
The line FirebaseUser user = firebaseAuth.getCurrentUser();
is correct because if i'm writing textView.setText("welcome "+ user.getEmail());
- I really 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.
它转到onCancelled()
.
推荐答案
您正在混合来自旧版 2.5.X Firebase SDK 的功能(示例:Firebase.setAndroidContext()) 和新的 9.XX SDK(示例:FirebaseAuth.getInstance()).这几乎肯定是失败的秘诀.重新编写代码以仅使用新的 SDK.设置指南在这里.
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 检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!