I am very new to firebase and I am unable to fetch the content of the key value. I tried out few things but all were unsuccessful.
Can anyone help me out in fetching the data please?
My firebase database - Firebase Database Structure:
My code -
public class Dashboard extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar dashboard_toolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private FirebaseAuth mAuth;
DatabaseReference rootRef, teacher, teacherPost;
String str;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
/* str = (String) getIntent().getExtras().get("temp");*/
textView = (TextView) findViewById(R.id.institute_news);
mAuth = FirebaseAuth.getInstance();
dashboard_toolbar = (Toolbar) findViewById(R.id.dashboard_toolbar);
setSupportActionBar(dashboard_toolbar);
getSupportActionBar().setTitle("Dashboard");
dashboard_toolbar.setTitleTextColor(0xFFFFFFFF);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawable);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, dashboard_toolbar, R.string.open, R.string.close);
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
mActionBarDrawerToggle.syncState();
dashboard_toolbar.post(new Runnable() {
@Override
public void run() {
Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.actionmenu, null);
dashboard_toolbar.setNavigationIcon(d);
}
});
NavigationView navigationView = (NavigationView) mDrawerLayout.findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
rootRef = FirebaseDatabase.getInstance().getReference();
teacher = rootRef.child("admin").child("classPost");
teacherPost = teacher.child("post");
String mGroupId = teacherPost.push().getKey();
Toast.makeText(this, "" + teacherPost, Toast.LENGTH_SHORT).show();
textView.setText(mGroupId);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawable);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nav_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mActionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.aboutUs) {
} else if (id == R.id.help) {
} else if (id == R.id.report) {
} else if (id == R.id.logoutmenu) {
mAuth.signOut();
sendToStart();
} else if (id == R.id.setting) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawable);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void getAttendance(View view) {
Toast.makeText(this, "Attendance Details Apk Fire here", Toast.LENGTH_LONG).show();
}
public void trackChild(View view) {
Toast.makeText(this, "child tracker apk fire here Here", Toast.LENGTH_LONG).show();
}
public void getEwallet(View view) {
Toast.makeText(this, "Ewallet Apk Fire Here", Toast.LENGTH_LONG).show();
}
public void getHealth(View view) {
Toast.makeText(this, "Health apk fire here", Toast.LENGTH_LONG).show();
}
public void getMessageBoardDetails(View view) {
startActivity(new Intent(this,MessageBoard.class));
}
public void startChat(View view) {
startActivity(new Intent(this,Chat.class));
}
private void sendToStart() {
startActivity(new Intent(Dashboard.this,Start.class));
finish();
}
public void readMore(View view) {
startActivity(new Intent(this,InstituteMessageBoard.class));
}
}
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("admin").child("classPost");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data : dataSnapshot.getChildren()){
String retrieveposts=data.child("posts").getValue().toString();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I'am assuming you want the post's values inside the classPost.
Explaining the code:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("admin").child("classPost");
The above is a reference to the location in the database. So child("admin").child("classPost"). You are referring to the location of the node classPost that is under the node admin, thus using child(node_name)
.
Now this:
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data : dataSnapshot.getChildren()){
String retrieveposts=data.child("posts").getValue().toString();
}
}
is a listener to be able to retrieve the values from the database. dataSnapshot
here is the classPost
. So when you write dataSnapshot.getChildren()
it means you are getting the children of classPost
and then data
will iterate inside the children thus giving you the values of posts
To achieve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classPostRef = rootRef.child("admin").child("classPost");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String post = ds.child("post").getValue(String.class);
Log.d("TAG", post);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
classPostRef.addListenerForSingleValueEvent(eventListener);
User contributions licensed under CC BY-SA 3.0