Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AlertDialog How to get the click result in outer class?

 
Sergey Lotvin
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day to All! I'll try to describe my question. It's something in architecture scope...as I believe. So, let's start.
I have an activity where the user can create new player or click "list" and get the list of already created players. So in activity.java I create DatabaseHelper and SQLiteDatabase and use it for INSERT (if new player) or SELECT (if the player choose the existing one). To fill the list I use "ListView" and "Adapter".
When user click (shortly) on the player name in the list, he get the AlertDialog with question "Do you want to proceed with player XXX?" If NO I simply cancel, if YES I take the name, create "intent" and start another activity with player name transferred (intent.putExtra("name", LoginDialog.selectedName)) to that another activity. That's quite good. But now I want to allow user to delete player. The idea is - after a long click user get AlertDialog with question "Do you really want to delete player XXX?" NO - it's clear - cancel. But how to cope with YES?

Roughly - after YES I have to connect to DB and perform DELETE query. How I tried:

public class DeleteDialog {
AlertDialog.Builder ad;
public static Context context;
public static String selectedName;
public DatabaseHelper mDatabaseHelper;
public SQLiteDatabase mSqLiteDatabase;
public void dialogOfSelectedName(Context context, String selectedName){

context=this.context;
selectedName=this.selectedName;
ad = new AlertDialog.Builder(context);
ad.setTitle("Удаление игрока");
ad.setMessage("Удалить игрока " + selectedName + "?");
ad.setCancelable(true);
mDatabaseHelper = new DatabaseHelper(context, "mydatabase.db", null, 1);
mDatabaseHelper.getReadableDatabase();
mSqLiteDatabase = mDatabaseHelper.getWritableDatabase();
ad.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSqLiteDatabase.execSQL("DELETE FROM" + DatabaseHelper.DATABASE_TABLE + " WHERE " + DatabaseHelper.NAME_COLUMN+" LIKE '"+selectedName+ "'");
}

});

ad.setPositiveButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

AlertDialog alert = ad.create();
alert.show();

}
}

The problem is "selectedName" cannot be seen from overridden method "onClick" but I need it in the query. How to realize it? Or my approach is totally wrong? Plese, help with advice.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic