[英]Android doesn't populate TextView with data saved in Firebase
I'm having a problem where I'm trying to create a landing page which is populated with data from a firebase database using previously created data. 我在尝试创建登录页面时遇到问题,该登录页面使用以前创建的数据填充了Firebase数据库中的数据。 I'm using code in the MainActivity here
我在MainActivity中使用代码
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth firebaseAuth;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
private TextView userName;
NavigationView navigationView = null;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FIREBASE LOGGED IN CHECK
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() == null){
finish();
startActivity(new Intent(this, LoginActivity.class));
}
FirebaseUser user = firebaseAuth.getCurrentUser();
//Set the fragment initially
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Nothing to see here, move along!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
userName = (TextView) headerView.findViewById(R.id.textViewUserName);
//GET INSTANCE OF DATABASE FROM URL
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReferenceFromUrl("https://userdata-57a47.firebaseio.com/");
DatabaseReference mChild = databaseReference.getRef().child("name");
mChild.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.getValue(String.class);
userName.setText(name);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I'm using the DatabaseReference to get the ID and that seems to work however when I try to output this information by converting it into a TextView using, the program doesn't seem to replace the Textview text with the 'name' field in my database 我正在使用DatabaseReference来获取ID,但似乎可以正常工作,但是当我尝试通过使用将其转换为TextView来输出此信息时,该程序似乎并没有将Textview文本替换为“ name”字段。数据库
Solved my own problem minutes after posting! 发布后几分钟解决了我自己的问题!
Simple fix, on the line where I put; 简单的修复,在我放置的位置;
DatabaseReference mChild = databaseReference.getRef().child("name");
I thought I was referencing my ID however getRef() is the wrong thing to use. 我以为我引用了我的ID,但是getRef()是错误的用法。 Instead I should have used .getUid().
相反,我应该使用.getUid()。 So now my code looks like this...
所以现在我的代码看起来像这样...
DatabaseReference mChild = databaseReference.child(user.getUid().child("name");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.