I'm a volunteer with very limited Java/Android Studio experience. This UpdateView.java has two "toggle" buttons. When one of these buttons is first pressed, a status and datetime is saved for that participant. If pressed again, the same button allows clearing of these values (in case a mistake was made). I would like to insert a Y/N confirmation but only before clearing the values. I read a lot of posts on AlertDialog and tried my best to implement this. However, when I press the button a second time to clear, I can see briefly the Yes/No dialog but it doesn't wait for keypress and performs the clear immediately. Any help would be greatly appreciated.
package com.org.suivicourse2.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.org.suivicourse2.Base.BaseActivity;
import com.org.suivicourse2.Database.DBHelper;
import com.org.suivicourse2.Model.Coureurs;
import com.org.suivicourse2.R;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class UpdateView extends BaseActivity {
ImageView back_btn;
TextView dossard_view;
TextView prenom_view;
TextView nom_view;
TextView sexe_view;
TextView epreuve_view;
TextView tempsPassage_view;
TextView statut_view;
LinearLayout exit_btn;
LinearLayout dnf_btn;
LinearLayout update_btn;
Coureurs model;
private DBHelper my_db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
initView();
}
private void initView(){
model = (Coureurs)getIntent().getSerializableExtra("model");
back_btn = findViewById(R.id.back_btn);
dossard_view = findViewById(R.id.dossard);
prenom_view = findViewById(R.id.Prenom);
nom_view = findViewById(R.id.Nom);
sexe_view = findViewById(R.id.Sexe);
epreuve_view = findViewById(R.id.Epreuve);
tempsPassage_view = findViewById(R.id.TempsPassage);
statut_view = findViewById(R.id.Statut);
exit_btn = findViewById(R.id.exit_btn);
dnf_btn = findViewById(R.id.dnf_btn);
update_btn = findViewById(R.id.update_btn);
back_btn.setOnClickListener(this);
exit_btn.setOnClickListener(this);
dnf_btn.setOnClickListener(this);
update_btn.setOnClickListener(this);
initValues();
}
private void initValues(){
dossard_view.setText(String.valueOf(model.getDossard()));
prenom_view.setText(model.getPrenom());
nom_view.setText(model.getNom());
sexe_view.setText(model.getSexe());
epreuve_view.setText(model.getEpreuve());
tempsPassage_view.setText(model.getTempsPassage());
statut_view.setText(model.getStatut());
}
@Override
public void onClick(View view) {
super.onClick(view);
if(view == back_btn){
finish();
}else if(view == exit_btn){
finish();
}else if(view == dnf_btn){
update_dnf();
}else if(view == update_btn){
update();
}
}
// PYT Button TEMPS as a toggle - Set/Clear DateTime for this racer
private void update(){
try{
SimpleDateFormat time_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String current_time = time_format.format(new Date(System.currentTimeMillis()));
String old_time = model.getTempsPassage();
String statut = model.getStatut();
if(statut.equals("En_Course")){
if (old_time != null && !old_time.equals("")) {
// Added following code for Yes/No confirmation
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
// End of code added for Yes/No confirmation
my_db = new DBHelper(context);
my_db.updateCoureurs(model.getDossard(), null);
showMessage("Temps de passage EFFACÉ pour ce coureur");
}else{
my_db = new DBHelper(context);
my_db.updateCoureurs(model.getDossard(), current_time);
//showMessage("Temps de passage assigné à ce coureur");
}
}
finish();
}catch (Exception e){
e.printStackTrace();
showMessage(getString(R.string.fail_update));
}
}
// PYT Button DNF as a toggle - Set/Clear DNF and DateTime for this racer
private void update_dnf(){
try{
SimpleDateFormat time_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String current_time = time_format.format(new Date(System.currentTimeMillis()));
String statut = model.getStatut();
String new_statut;
if(statut.equals("En_Course")){
new_statut = "DNF";
my_db = new DBHelper(context);
my_db.dnfCoureurs(model.getDossard(), current_time, new_statut);
showMessage("Statut DNS/DNF assigné à ce coureur");
}else{
new_statut = "En_Course";
my_db = new DBHelper(context);
my_db.dnfCoureurs(model.getDossard(), current_time, new_statut);
showMessage("Statut En_Course ré-assigné à ce coureur");
}
finish();
}catch (Exception e){
e.printStackTrace();
showMessage(getString(R.string.fail_update));
}
}
}
CodePudding user response:
Those 3 lines need to go into the click listener of your yes/no dialog:
my_db = new DBHelper(context);
my_db.updateCoureurs(model.getDossard(), null);
showMessage("Temps de passage EFFACÉ pour ce coureur");
CodePudding user response:
I'm only editing the update
function
private void update(){
try{
SimpleDateFormat time_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String current_time = time_format.format(new Date(System.currentTimeMillis()));
String old_time = model.getTempsPassage();
String statut = model.getStatut();
if(statut.equals("En_Course")){
if (old_time != null && !old_time.equals("")) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("Are you sure?")
.setPositiveButton("Yes", (dialogInterface, i) -> {
my_db = new DBHelper(context);
my_db.updateCoureurs(model.getDossard(), null);
showMessage("Temps de passage EFFACÉ pour ce coureur");
})
.setNegativeButton("No", (dialogInterface, i) -> {
dialogInterface.cancel();
}).show();
}else{
my_db = new DBHelper(context);
my_db.updateCoureurs(model.getDossard(), current_time);
showMessage("Temps de passage assigné à ce coureur");
}
}
finish();
}catch (Exception e){
e.printStackTrace();
showMessage(getString(R.string.fail_update));
}
}