Reputation: 25
So basically I am building an app that will display events on a screen and update it as soon as a specific time which has been set has been reached. It would be easier to use a timer instead of Cloud Functions to achieve this but when I tried, it just didn't work. Looked at other questions on StackOverFlow but all weren't clear. What am I doing wrong? Below is my code.
update_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int time = 0;
String info = information.getEditText().getText().toString();
final long timeCountInMilliSeconds = time * 60 * 1000;
time = Integer.parseInt(xxx.getEditText().getText().toString().trim());
if (!TextUtils.isEmpty(info) && !TextUtils.isEmpty(String.valueOf(time))) {
progressBar.setVisibility(View.VISIBLE);
update_ref = FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
final int finalTime = time;
update_ref.setValue(info).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
void onDataChanged(@NonNull DataSnapshot snapshot) {
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
retrieve_ref.setValue("No event scheduled");
}
},
timeCountInMilliSeconds);
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Toast.makeText(AnkobraOneActivity.this, "Event Changed and scheduled for " + finalTime + " hours", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.INVISIBLE);
myDialog.dismiss();
}
});
} else
{
Toast.makeText(AnkobraOneActivity.this, "Insert all details accordingly", Toast.LENGTH_SHORT).show();
}
}
});
I used this answer to achieve what I have done so far but it didn't work.
Help?
Upvotes: 0
Views: 1936
Reputation: 1498
based on your code,
int time = 0;
String info = information.getEditText().getText().toString();
final long timeCountInMilliSeconds = time * 60 * 1000;
.
timeCountInMilliSeconds
always =0;
so, you have to change the code to
int time = 0;
String info = information.getEditText().getText().toString();
time =Integer.parseInt(xxx.getEditText().getText().toString().trim());
final long timeCountInMilliSeconds = time * 60 * 1000;
Also, you have to use setValue()
inside the timer method.
Here is the complete code:
update_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int time = Integer.parseInt(xxx.getEditText().getText().toString().trim());
String info = information.getEditText().getText().toString();
final long timeCountInMilliSeconds = time * 60 * 1000;
if (!TextUtils.isEmpty(info) && !TextUtils.isEmpty(String.valueOf(time))) {
progressBar.setVisibility(View.VISIBLE);
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
update_ref =
FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
final int finalTime = time;
update_ref.setValue(info).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
final DatabaseReference ref =
FirebaseDatabase.getInstance().getReference().child("Event Halls").child("Ankobra One").child("Event Schedule");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
void onDataChanged(@NonNull DataSnapshot snapshot) {
retrieve_ref.setValue("No event scheduled");
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Toast.makeText(AnkobraOneActivity.this, "Event Changed and scheduled for " + finalTime + " hours", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.INVISIBLE);
myDialog.dismiss();
}
});
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
timeCountInMilliSeconds);
} else
{
Toast.makeText(AnkobraOneActivity.this, "Insert all details accordingly", Toast.LENGTH_SHORT).show();
}
}
});
Upvotes: 1