0
我遇到了一個問題,我嘗試使用先前創建的數據創建一個使用來自Firebase數據庫的數據填充的着陸頁。我使用這裏在MainActivityAndroid無法使用Firebase中保存的數據填充TextView
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) {
}
});
我使用DatabaseReference獲取的ID碼,這似乎通過,該方案沒有按它轉換成一個TextView,當我嘗試將該信息輸出到然而工作似乎沒有用我的數據庫中的'名稱'字段替換文本視圖文本