0

How can I delete a row in listview after pressing YES button from alertDialog buttons?!

in my code Cancel is a button in row I want if i press it, it should do two things:

  1. Show me alertDialog : i did that by calling a function ShowDialogCancel() ,
  2. After pressing: YES in this alertDialog ==> the row should be removed !!

I tried with many ways but it delete the row before I press YES button in the alertDialog

any Suggestion

Monerah ...

My adapter class:

public class MyCasesListAdapter extends BaseAdapter {
    private MyPage myPage;
    private List<MyCaseClass> listOfCases;
    private Activity parentActivity;

    // TODO test
    MyCaseClass entry;

    // TODO delete it not imp.
    public MyCasesListAdapter() {

        super();

    }

    public MyCasesListAdapter(MyPage mypage, List<MyCaseClass> listOfCaseParameter, Activity parentActivity) {
        this.myPage = mypage;
        this.listOfCases = listOfCaseParameter;
        this.parentActivity = parentActivity;
    }
...
    public View getView(int position, View convertView, ViewGroup viewGroup) {

         entry = listOfCases.get(position);
         //this.getitem(position)
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) myPage
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mypage_row, null);
          }


        // this is row items..
        // Set the onClick Listener on this button
        Button ConfExpandRegion = (Button) convertView.findViewById(R.id.expand);
        Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
        TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);

        // To be a clickable button
        ConfExpandRegion.setFocusableInTouchMode(false);
        ConfExpandRegion.setFocusable(false);


        ConfExpandRegion.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                myPage.ShowingDialogExpand();
            }
        });

        // To be a clickable button
        Cancelb.setFocusableInTouchMode(false);
        Cancelb.setFocusable(false);
        Cancelb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                entry = (MyCaseClass) v.getTag();
                int caseid= entry.getID();
                myPage.ShowingDialogCancel(caseid);

                Toast.makeText(myPage, "inside calling", 0).show();

                //MyCaseClass entry = (MyCaseClass) v.getTag();
                //listOfCases.remove(entry);

                // listPhonebook.remove(view.getId());
                notifyDataSetChanged();
            }
        });

        // Set the entry, so that you can capture which item was clicked and
        // then remove it
        // As an alternative, you can use the id/position of the item to capture
        // the item
        // that was clicked.
        ConfExpandRegion.setTag(entry);
        Cancelb.setTag(entry);

        // btnRemove.setId(position);

        return convertView;
    }

    public void onClick(View view) {
        MyCaseClass entry = (MyCaseClass) view.getTag();
        listOfCases.remove(entry);
        // listPhonebook.remove(view.getId());
        notifyDataSetChanged();

    }


}

From my page class:

public void  ShowingDialogCancel(){
        final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
        alertDialog2.setTitle("Conformation?");
        alertDialog2.setMessage("Are you sure you want to cancel x cases?");


        alertDialog2.setButton("Yes", new DialogInterface.OnClickListener() {
               public  void onClick(DialogInterface dialog, int which) {

                     CancelMsg = "Case_ID cancel";


                   if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above 
                            sendSMS("0000", CancelMsg);
                            Toast.makeText(MyPage.this, "Cancelation request sent", Toast.LENGTH_LONG)
                                    .show();

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(MyPage.this, e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                   }
            });

            alertDialog2.setButton2("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                // Do nothing 

                   Toast.makeText(MyPage.this, "inside No", 0)
                    .show();


               }
            });

            alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog2.show();

    }

}
Sam
  • 86,580
  • 20
  • 181
  • 179
Monerah
  • 215
  • 1
  • 5
  • 17
  • 2
    Start variable names in lower case, otherwise they are colored like class names (and by convention instance variable names starts in lowercase). – Chopin Apr 28 '12 at 19:31

3 Answers3

0

How are you populating your listview? If you are using an array you can remove the data from your array and reload your list view.

Anila
  • 1,136
  • 2
  • 18
  • 42
  • in my Adapter class I put a code in Cancel button, that clear the list but i didn't work ... can you explain more =) – Monerah Apr 28 '12 at 19:36
0

If you have the position you want to remove, you can call

listOfCases.remove(position);
notifyDataSetChanged();
Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
0

Using the code you have, if you pass references to ShowingDialogCancel(MyCasesListAdapter adapter, View view) then you can add something like:

adapter.onClick(view);

to your "Yes" button.

Sam
  • 86,580
  • 20
  • 181
  • 179