2014-01-31 30 views
0

我在我的活動中調用方法ShowDialog。我有一個名稱列表,所以我將在這個名稱列表中循環,併爲每個名稱,我會得到它的數字。有些名字可能只有一個數字,其他的名字可能有更多。如果一個名稱的數量超過一個,我會顯示另一個對話框,提示您選擇所需的數字。Android - 另一個對話框內的對話框 - 如何知道它們何時完成切除?

當所有的名字都只有一個數字,它是容易的,我叫dialog.dismiss,然後SendSMS(listOfNumbers),並最終完成():

ShowDialog(); 
SendSMS(listOfNumbers); 
finish(); 

但是,當用戶擁有多個號碼出現問題。當過程結束時,我無法找到讓我知道的方法。

的ShowDialog()方法:

protected void ShowDialog() { 

    final Dialog dialog = new Dialog(ContactsPicker.this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.alert_dialog); 

    final CheckBox cbE = (CheckBox) dialog.findViewById(R.id.checkBoxEmail); 
    final CheckBox cbN = (CheckBox) dialog.findViewById(R.id.checkBoxNumber); 

    Button button = (Button) dialog.findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String names = ""; 
      String names1 = ""; 
      String names2 = ""; 

      if (cbN.isChecked()) { 
       //Send invitations through SMS 
       //List<String> numbers = new ArrayList<String>(); 
       String number = ""; 
       List<String> usernumbers = new ArrayList<String>(); 

       //Fill names string to send it back 
       for (int i = 0; i < selected.size(); i++) { 
        //number = get_Number(contactName, getApplicationContext()); 

        usernumbers = getNumber(selected.get(i), getApplicationContext()); 

        //Toast.makeText(getApplicationContext(),"Usernumbers list size: "+usernumbers.size(),Toast.LENGTH_SHORT).show(); 

        if (usernumbers.size() == 1) { 
         selectedNumbers.add(usernumbers.get(0)); 
        } else if (usernumbers.size() > 1) { 
         //Show select number dialog 
         dialog.hide(); 
         showSelectNumberPopup(usernumbers); 
        } else { 
        } 
        //dialog.show(); 
       } 
      } 

      if (cbE.isChecked()) { 
       //Send invitations through e-mail 
       List<String> emails = new ArrayList<String>(); 
       String email = ""; 

       for (String contactName : selected) { 
        email = get_Email(contactName, getApplicationContext()); 
        emails.add(email); 
        names2 += contactName + ","; 
       } 

       //showToast("Send E-mail Checked"); 
       sendEmail(emails); 
      } 
     } 
    }); 
} 

而且showSelectNumberPopup方法:

protected void showSelectNumberPopup(List<String> usernumbers) { 
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.alert_dialog_select_number, null); 
    // vi = LayoutInflater.from(getBaseContext()).inflate(R.layout.alert_dialog_select_number,null); 
    //View view = (View)getLayoutInflater().inflate(R.layout.alert_dialog_select_number, null); 
    dialogLV = (ListView) view.findViewById(R.id.list_select_number); 
    dialogLV.setAdapter(new SelectNumberDialogListAdapter(getApplicationContext(), usernumbers)); 
    dialogLV.setItemsCanFocus(false); 
    dialogLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    dialogLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) { 
      String selectedFromList = (String) (dialogLV.getItemAtPosition(myItemInt)); 
      cbSelectedNumber = (CheckBox) myView.findViewById(R.id.checkBoxNumber); 
      if (selectedNumbers.contains(selectedFromList)) { 
       selectedItemCB.setChecked(false); 
       selectedNumbers.remove(selectedFromList); 
       //Toast.makeText(getApplicationContext(), "Removed From SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show(); 
      } else { 
       selectedItemCB.setChecked(true); 
       selectedNumbers.add(selectedFromList); 
       //Toast.makeText(getApplicationContext(), "Added To SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    final Dialog dialog = new Dialog(ContactsPicker.this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(view); 
    //Toast.makeText(getApplicationContext(),"GOT IN THE SECOND POPUP",Toast.LENGTH_SHORT).show(); 
    Button button = (Button) dialog.findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 
    dialog.show(); 
    if (names1 != "") 
     names = names1; 
    else if (names2 != "") 
     names = names2; 
    else { 
     for (String contactName : selected) { 
      names += contactName + ","; 
     } 
    } 
    names = names.substring(0, names.lastIndexOf(",")); 
    //Set data to send it back to PickNamesActivity 
    Intent data = new Intent(); 
    //---set the data to pass back--- 
    data.setData(Uri.parse(names)); 
    setResult(RESULT_OK, data); 
    //finish(); 
    //sendSMS(selectedNumbers); 
    //finish(); 
    dialog.show(); 
} 
+0

你可以使用dialog.isShowing()方法來檢查它是否可見或被忽略。 –

+0

第二個對話框可能會被多次調用,那麼isShowing應該如何幫助? – Hussain

回答

0

TL; DR:Use an interface

什麼可能最終會被更容易爲你是創建兩個(內)擴展DialogFragment並使用new FirstDialog().show()調用這些類的對話框類。將關聯的代碼移到這些對話框中。例如:

private class FirstDialog extends DialogFragment { 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     View view = getLayoutInflater().inflate(R.layout.alert_dialog, null); 
     final CheckBox cbE = (CheckBox)view.findViewById(R.id.checkBoxEmail); 
     final CheckBox cbN = (CheckBox)view.findViewById(R.id.checkBoxNumber); 

     return new AlertDialog.Builder(this) 
       .setPositiveButton("Text here", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         //Put your on click stuff here 
        } 
       }).create(); 

    } 

} 

private class SecondDialog extends DialogFragment{ 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     View view = getLayoutInflater().inflate(R.layout.alert_dialog_select_number, null); 
     dialogLV = (ListView)view.findViewById(R.id.list_select_number); 
     dialogLV.setAdapter(new SelectNumberDialogListAdapter(getApplicationContext(),usernumbers)); 
     dialogLV.setItemsCanFocus(false); 
     dialogLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     dialogLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) { 

       String selectedFromList = (String) (dialogLV.getItemAtPosition(myItemInt)); 
       cbSelectedNumber = (CheckBox)myView.findViewById(R.id.checkBoxNumber); 

       if (selectedNumbers.contains(selectedFromList)) { 
        selectedItemCB.setChecked(false); 
        selectedNumbers.remove(selectedFromList); 
        //Toast.makeText(getApplicationContext(), "Removed From SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show(); 

       } else { 
        selectedItemCB.setChecked(true); 
        selectedNumbers.add(selectedFromList); 
        //Toast.makeText(getApplicationContext(), "Added To SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }); 

     return new AlertDialog.Builder(this) 
       .setView(view) 
       .setPositiveButton("Text here", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         //What happens when the button is clicked 
        } 
       }).create(); 



    } 

不知道什麼所有的代碼做了或你的對話是如何建立的,但希望這個基地可以適合你的東西休息。當你想從第二個對話框中取回某些東西時,你可以爲它添加一個接口,在你的Activity內部調用一個方法來完成它需要做的事情。 Here's an Android Developer link to help you with that.

+0

非常感謝。這是信息。我得到了一個解決方案,稍後我會確保它可以100%正常工作。 – Hussain