Reputation: 897
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. I'm using code in the MainActivity here
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
Upvotes: 0
Views: 198
Reputation: 897
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. Instead I should have used .getUid(). So now my code looks like this...
DatabaseReference mChild = databaseReference.child(user.getUid().child("name");
Upvotes: 1