How to start another activity when value is added in Firebase - android

I'd like to start another activity when a value is added in the database of Firebase.
No error is appearing but when I add a value in the database nothing happens.
Here's my sample code. I've been searching for the past days but it is my first app and I'm a bit confused.
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blackscreen);
databaseReference = FirebaseDatabase.getInstance().getReference();
}
#Override
protected void onStart() {
super.onStart();
usingFirebaseDatabase();
}
public void usingFirebaseDatabase() {
databaseReference.child("uploads")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
do {
if (dataSnapshot.exists()) {
Intent intent = new Intent(blackscreen.this,MainActivity.class);
startActivity(intent);
} else {
return;
}
} while (!dataSnapshot.exists());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(blackscreen.this, "NO images found \n" + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}

Related

Firebase Value Event listener being skipped

When debugging, the program doesn't get into user ref value event listener why would this happen?
I get rid = null but the firebase instantiation is correct from what I can tell.
[text so stack overflow lets me post since there's too much code and no further explanation for the issue]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message_chat);
getSupportActionBar().hide();
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
useref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot snap : snapshot.getChildren()) {
rid = snap.child("receiverid").getValue(String.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException();
}
});
rv = findViewById(R.id.rvcahtitems);
b = findViewById(R.id.chatsendb);
field = findViewById(R.id.chatfield);
user = FirebaseAuth.getInstance().getCurrentUser();
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String msg = field.getText().toString();
if ((!msg.equals("")))
{
SendM(user.getUid(), rid, msg);
}
else
{
Toast.makeText(ChatScreen.this, "Can't send an empty message", Toast.LENGTH_SHORT);
}
field.setText("");
}
});
rv.setLayoutManager(new LinearLayoutManager(this));
readmsgs(userID, rid);
}

Get second Onclicklistener values

I have String values in firebase database
I would like to get this String
in onClick i make a toast for display the value but the value is toasted in the second click
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_souslistecuisine);
cuisinecomplet=(ImageView)findViewById(R.id.cuisinecomplett);
FirebaseDatabase database= FirebaseDatabase.getInstance();
DatabaseReference myRef=database.getReference("Produit");
DatabaseReference key=myRef.child("Cuisine");
DatabaseReference text=key.child("cuisine complet");
text1=text.child("text1");
text1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
txt=dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
public void complet(View view) {
Toast.makeText(souslistecuisine.this, txt, Toast.LENGTH_LONG).show();
}
I tried to put text1.addValueEventListener(new ValueEventListener()) in Onclick method but same result
Just add the Toast after you already receive the String value from Firebase, as Firebase callbacks run in background thread and take time to response.
text1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
txt=dataSnapshot.getValue(String.class);
Toast.makeText(souslistecuisine.this, txt, Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Not able to retrieve data from Firebase real time database in Android studio

I'm new to the firebase and android studio. I'm working on a project in which I have to fetch details of the current user and display the name. It is throwing "user doesn't exist' toast even if I'm logged in I'm attaching everything below. Maybe I have done some mistakes please let me know.
Code to fetch details of the current user
public class Succesfully_sign_up extends AppCompatActivity {
DatabaseReference reff;
TextView name;
Button signout;
FirebaseUser curr_user;
String curr_user_str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_succesfully_sign_up);
name = findViewById(R.id.name);
curr_user=FirebaseAuth.getInstance().getCurrentUser();
if(curr_user!=null)
{
curr_user_str = curr_user.getUid();
reff = FirebaseDatabase.getInstance().getReference("userdetails");
reff.child(curr_user_str).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
HelperClass data = dataSnapshot.getValue(HelperClass.class);
if (data != null) {
name.setText(data.getFullname_hc());
} else {
Toast.makeText(Succesfully_sign_up.this, "User does't exist", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
else
{
startActivity(new Intent(Succesfully_sign_up.this,student_signup.class));
}
}
}
My HelperClass
public class HelperClass {
String fullname_hc,amizone_id_hc,mobile_num_hc,email_id_hc;
public HelperClass()
{
}
public HelperClass(String fullname_hc, String amizone_id_hc, String mobile_num_hc, String email_id_hc) {
this.fullname_hc = fullname_hc;
this.amizone_id_hc = amizone_id_hc;
this.mobile_num_hc = mobile_num_hc;
this.email_id_hc = email_id_hc;
}
public String getFullname_hc() {
return fullname_hc;
}
public void setFullname_hc(String fullname_hc) {
this.fullname_hc = fullname_hc;
}
public String getAmizone_id_hc() {
return amizone_id_hc;
}
public void setAmizone_id_hc(String amizone_id_hc) {
this.amizone_id_hc = amizone_id_hc;
}
public String getMobile_num_hc() {
return mobile_num_hc;
}
public void setMobile_num_hc(String mobile_num_hc) {
this.mobile_num_hc = mobile_num_hc;
}
public String getEmail_id_hc() {
return email_id_hc;
}
public void setEmail_id_hc(String email_id_hc) {
this.email_id_hc = email_id_hc;
}
}
I think you have created the database manually, your user id 8448.... is not generated by the Firebase android, so it is showing that error. If not then please share the code of creation of that above database structure.
How about looping through the children and accessing data:
//the reference
reff = FirebaseDatabase.getInstance().getReference("userdetails");
//the reading
reff.child.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//loop through every possible child under "userdetails"
for(DataSnapshot ds : dataSnapshot.getChildren()){
String fullName = ds.child("fullname_hc").getValue(String.class);
name.setText(fullName);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Firebase Database reopens activity when a value changes

I have a problem in my activity when I change a value in the database. I tried creating a new Activity with a toolbar, the toolbar's title is changed with a database value via ValueEventListener. The problem is that when I change the value in my DB, the activity reopens itself.
private static String eventKey;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() == null) {
finishAffinity();
startActivity(new Intent(this, HomeScreenActivity.class));
}
eventKey = getIntent().getExtras().getString("eventKey");
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Eventos").child(eventKey);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String titleValue = dataSnapshot.child("title").getValue().toString();
getSupportActionBar().setTitle(titleValue);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
This is the full code of the TestingActivity class.
Here you can see the previous activity (is too large). And here you can see the log when I change something in my DB.
Thanks!
By looking your code provided in the link, it seems you are launching the TestingActivity or EventSingleActivity.
This is the code from the link:
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDatabase.child(id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String eventTime = dataSnapshot.child("timeStamp").getValue().toString();
if(Integer.parseInt(nowTime) > Integer.parseInt(eventTime)){
Intent intent = new Intent(PrincipalActivity.this, TestingActivity.class);
intent.putExtra("eventKey", id);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(PrincipalActivity.this, EventSingleActivity.class);
intent.putExtra("eventKey", id);
startActivity(intent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
That's why every changes from the firebase db it will launch the activities I mentioned above.
To fix this you need to do the following:
mDatabase.child(id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String eventTime = dataSnapshot.child("timeStamp").getValue().toString();
if(Integer.parseInt(nowTime) > Integer.parseInt(eventTime)){
Intent intent = new Intent(PrincipalActivity.this, TestingActivity.class);
intent.putExtra("eventKey", id);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(PrincipalActivity.this, EventSingleActivity.class);
intent.putExtra("eventKey", id);
startActivity(intent);
}
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Retrieving data from a firebase database with multiple nodes

I was trying to retrieve the data from user and time. But I can't access the data, it's not really accessing the database.
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
mFirebaseDatabase=FirebaseDatabase.getInstance();
cashRef=mFirebaseDatabase.getReference().child("ASSETS").child("cash_at_bank");
TextView view=(TextView)findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cashRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int time=dataSnapshot.getValue(int.class);
int values =dataSnapshot.getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
To get the time, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference cashAtBankRef = rootRef.child("ASSETS").child("Cash at bank");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Long time = ds.child("time").child("time").getValue(Long.class);
Log.d("TAG", time + "");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
cashAtBankRef.addListenerForSingleValueEvent(eventListener);
The output will be:
150555043995
//and so on

Categories

Resources