Hello, I need help with android programming. The problem is that I can't make this portion of my codes to work the way I want to. I want to simply:
- Delete a record inside the SQL lite database
- Delete the record also inside the AlertDialog Single Choice Item list
That's it. I am trying to solve this one right now. Trying to see where the fault is. My android app works well on my phone with no detected errors only that it won't let me delete a single record. Must the problem be with the declaration of Array or from the SQL statement?
String getPlayer=""; //to be used later to store Player's name
public void revealWhenButtonRemove(){
AlertDialog.Builder revealListOfNames = new AlertDialog.Builder(userProfile.this);
ArrayList<String> ar = new ArrayList<String>();
Cursor c=db.rawQuery("SELECT * FROM player", null);
while(c.moveToNext())
{
ar.add(c.getString(0)+"\n"); //add to arrayList
}
Object[] objRevAr=ar.toArray();
final String[] revAr= Arrays.copyOf(objRevAr, objRevAr.length, String[].class); // put into array revAr to display on List
if(revAr.length == 0)
{ // if there is no saved Player Data
revealListOfNames.setTitle("Awww.. :(");
revealListOfNames.setMessage("No one is here yet");
revealListOfNames.setNeutralButton("Oh, Okay..", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); // Just cancel this dialog
}
});
}
else
{ // if there are players / is a player
revealListOfNames.setTitle("Hmm.. which one to remove?");
revealListOfNames.setSingleChoiceItems(revAr, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getPlayer = revAr[which].toString(); //save selected Player name to getPlayer variable for assessment later
}
});
revealListOfNames.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { //if user chooses to remove a player on the list
db.execSQL("DELETE FROM player WHERE playerName='" + getPlayer + "'"); //Line of code to delete record with the same name as with variable getPlayer
Toast.makeText(getApplicationContext(), "Aww.. " +getPlayer+" has been removed :(", Toast.LENGTH_LONG).show();
}
});
revealListOfNames.setNegativeButton("Nah..", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { //if user chooses to cancel dialog
dialog.cancel();
Toast.makeText(getApplicationContext(), "Uh.. Okay! :)",
Toast.LENGTH_SHORT).show();
}
});
}
AlertDialog alertDialog = revealListOfNames.create();
// show alert
alertDialog.show();
}