Reputation: 109
I am trying to create a dialogAlert to check if the user wants to delete a workout but I can't figure out where everything should go in relation to my onclicklistner
. I have tried moving onclicklistener around but don't know where it should be
Here is my AndroidListViewCursorAdaptorActivity class:
public class AndroidListViewCursorAdaptorActivity extends Activity {
private ListView lv;
private DBAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workouts);
dbHelper = new DBAdapter(this);
dbHelper.open();
//Generate ListView from SQLite Database
displayListView();
ListView listView = (ListView) findViewById(R.id.listView1);
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
builder1.setMessage("Are you sure you want to delete this workout?.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
Toast.makeText(getApplicationContext(),
"You have deleted a workout!", Toast.LENGTH_LONG).show();
Log.v("long clicked", "pos: " + pos);
dbHelper.deleteRow(id);
displayListView();
return true;
}
});
}
private void displayListView() {
Cursor cursor = dbHelper.getAllRecords();
// The desired columns to be bound
String[] columns = new String[] {
DBAdapter.KEY_TITLE,
DBAdapter.KEY_WORKOUTDATE,
DBAdapter.KEY_EXERCISE_NOTES,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.title,
R.id.workoutDate,
R.id.workoutDetails,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.workout_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
}
Upvotes: 0
Views: 142
Reputation: 4661
try this,
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
getUserConfirmation(id);
return true;
}
});
public void getUserConfirmation(long idSelected){
AlertDialog.Builder builder1 = new AlertDialog.Builder(AndroidListViewCursorAdaptorActivity.this);
builder1.setMessage("Are you sure you want to delete this workout?.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(AndroidListViewCursorAdaptorActivity.this,
"You have deleted a workout!", Toast.LENGTH_LONG).show();
dbHelper.deleteRow(idSelected);
displayListView();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
Upvotes: 2
Reputation: 3812
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long id) {
//this is where you want the user to confirm, so you show the dialog
showDeleteConfirmationDialog(AndroidListViewCursorAdaptorActivity.this, id);
return true;
}
});
The dialog code is moved to a helper function ...
private static void showDeleteConfirmationDialog(Context context, long id){
new AlertDialog.Builder(context)
.setTitle("Delete Workout")
.setMessage("Are you sure you want to delete this workout?.")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(context,"You have deleted a workout!", Toast.LENGTH_LONG).show();
dbHelper.deleteRow(id);
displayListView();
dialog.dismiss();
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do not delete, ;
dialog.dismiss();
}
}).show();
}
Upvotes: 0
Reputation: 14825
Try this and if you have any problem let me know
listview.onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(Activity.this);
alert.setTitle("Delete");
alert.setMessage("Are you sure to delete");
alert.setPositiveButton("YES", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"You have deleted a workout!", Toast.LENGTH_LONG).show();
Log.v("long clicked", "pos: " + pos);
dbHelper.deleteRow(id);
displayListView();
dialog.dismiss();
}
});
alert.setNegativeButton("NO", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
return true;
}
});
Upvotes: 0
Reputation: 4623
You are canceling dialog when user press yes, instead put logic to delete item here.
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
//write logic to delete item here.
dbHelper.deleteRow(id);
displayListView();
dialog.dismiss();
}
});
Upvotes: 0