lstv = (ListView) findViewById(R.id.lista);
lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int posicion, long id) {
removeItemFromList(posicion);
}
});
}
protected void removeItemFromList(final int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
Apple.this);
alert.setTitle("Delete");
alert.setMessage("¿Do you want delete?");
alert.setPositiveButton("Si", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Help here please...!!
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
-
Which data set you are using to fill your adapter of your listview, array or arraylist ??A.R.– A.R.2016-08-06 19:41:32 +00:00Commented Aug 6, 2016 at 19:41
-
I´m using array list, but i dont sure :/Andy R– Andy R2016-08-06 19:48:49 +00:00Commented Aug 6, 2016 at 19:48
-
Of what thing you are not sure ?A.R.– A.R.2016-08-06 19:50:58 +00:00Commented Aug 6, 2016 at 19:50
-
My listview is the result of a selection in a separate activity , then I want to delete the item with dialogAndy R– Andy R2016-08-06 19:55:02 +00:00Commented Aug 6, 2016 at 19:55
-
Although the result is in separate activity, you are storing your result in array list which you are setting as data source in adapter of listview. Thus, you can use "arrayList.remove(position)" from dialog and update or refresh your listviewA.R.– A.R.2016-08-06 20:02:06 +00:00Commented Aug 6, 2016 at 20:02
|
Show 1 more comment
3 Answers
you need to pass also the arraylist of item to the function and after you removed you need to run the function notifyDataSetChanged(); this function re-build the listview.
so:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
alertDialog.setTitle("title...");
alertDialog.setMessage("Are you sure you want delete this?");
alertDialog.setIcon(R.drawable.delete);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
mArrayList.remove(position);
mArrayAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
Comments
Add this code in your Alert.Dialog methods
alert.setPositiveButton("Si", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
itemList.remove(favTransPosition);
adapter.notifyDataSetChanged();
adapter = new ArrayAdapter<String>(, , , itemList); // Fill remaining params as you have created earlier
lstv.setAdapter(adapter);
}
});
3 Comments
Andy R
Not jet, i have doubts in the adapter = new ArrayAdapter<String>(, , , itemList); and i get error in adapter.notifyDataSetChanged();
A.R.
you can replace "adapter = new ArrayAdapter<String>(, , , itemList)" with yours which you have defined above in the code. What error you are getting in adapter.notifyDataSetChanged()
Andy R
just looks in red, i'll try :)