2012-04-28 9 views
0

alertDialogボタンからYESボタンを押した後、リストビュー内の行を削除するにはどうすればいいですか?私のコードCancelListViewで行を削除する[アンドロイドアプリケーション]

は、私はそれを押すと、それは二つのことを行う必要があり、私がしたい行のボタンです:

  1. は私をalertDialog表示:私は押した後
  2. 、機能ShowDialogCancel()を呼び出していることをやりました:このalertDialogでYES ==>行を削除する必要があります!!

...私は多くの方法で試してみましたが、私はalertDialog

でYESどんな提案

Monerahを、ボタンを押す前に、行を削除

マイアダプタクラス

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(); 

    } 


} 

私のページのクラスから:

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(); 

    } 

} 
+2

変数名を小文字で開始します。そうでない場合は、クラス名のように色付けされます(通常、インスタンス変数名は小文字で始まります)。 – Chopin

+0

この情報に感謝します。 – Monerah

答えて

0

あなたはどのようにリストビューを作成していますか?配列を使用している場合、配列からデータを削除し、リストビューをリロードすることができます。

adapter.onClick(view); 

:あなたが削除したい位置を持っている場合は、あなたのようなものを追加することができますShowingDialogCancel(MyCasesListAdapter adapter, View view)への参照を渡す場合、あなたは、あなたが持っているコードを使用

listOfCases.remove(position); 
notifyDataSetChanged(); 
+0

私のアダプタクラスでは、キャンセルボタンにコードを入れてリストをクリアしましたが、うまくいきませんでした... – Monerah

0

あなたの "はい"ボタンに。

+0

私はこれを実行して正しく削除しましたが、ユーザーがAlertDialogのYESボタンを押したときに起こった – Monerah

+0

あなたの答えにとても感謝しています。 – Monerah

+0

ダイアログのOKボタン用の 'DialogInterface.OnClickListener'の中にコードを入れてください。 –

0

を呼び出すことができます

関連する問題