I am using firebase realtime database in my application. If the data I send with Intent is empty, the application closes. If the data in the intent is empty, how can I connect to the database and pull data?
String post_title ;
post_title = getIntent().getExtras().get("post_title").toString();
txttitle.setText(post_title);
if post_title is null i want it to do this:
databaseReference = FirebaseDatabase.getInstance().getReference().child("AllPost").child(PostKey);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("title")){
String title = dataSnapshot.child("title").getValue().toString();
txttitle.setText(title);
}
I tried this:
if (post_title == null || post_title.isEmpty()) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.hasChild("title")) {
String title = snapshot.child("title").getValue().toString();
txttitle.setText(title);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
} else {
txttitle.setText(post_title);
}