Reputation: 5935
I am developing an android application in which i have a static listview which contains 5 items.
I want that when i click on selected item an alert dialog should appear containing buttons yes or no. If i click on yes, selected item should get deleted.
Is it possible? If yes, how? Can anyone guide me.The data in the list is static
Here is my java class code
Upvotes: 1
Views: 101
Reputation: 4580
ArrayList<String> sampleList;
ArrayAdapter<String> sampleListArrayAdapter;
ListView sampleListView = (ListView) findViewById(R.id.sampleListView);
sampleListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(
SampleActivity.this);
builder.setMessage("Do you want delete this item?");
builder.setCancelable(false);
// On clicking "Yes" button
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
sampleList.remove(position);
sampleListArrayAdapter.notifyDataSetChanged();
});
// On clicking "No" button
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); // Cancel dialog
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
Upvotes: 2
Reputation: 11251
When the user chooses to delete the row, get the row number where user wanted this deletion to happen, refer to the corresponding element of your source array, remove that element and then call Adapter.notifyDataSetChanged()
Upvotes: 0
Reputation: 4443
Remove the item from the data list and notify the list adapter about the change.
data.remove(Item);
notifyDataSetChanged();
Alert dialog tutorial is here: http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
Upvotes: 0
Reputation: 8242
call adapter.clear() followed by adapter.notifyDataSetChanged() when you want to delete the elements.
Upvotes: 0