0
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();
6
  • Which data set you are using to fill your adapter of your listview, array or arraylist ?? Commented Aug 6, 2016 at 19:41
  • I´m using array list, but i dont sure :/ Commented Aug 6, 2016 at 19:48
  • Of what thing you are not sure ? Commented 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 dialog Commented 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 listview Commented Aug 6, 2016 at 20:02

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

Refer this

Modify arrayList

and then

arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();

Now if required again set adapter to the ListView

1 Comment

I have ArrayList<String> itemList; ArrayAdapter<String> adapter; like global variables
0

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

Not jet, i have doubts in the adapter = new ArrayAdapter<String>(, , , itemList); and i get error in adapter.notifyDataSetChanged();
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()
just looks in red, i'll try :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.