2016-12-29 63 views
1

所以我有一個AlertDialog這使得MultiChoiceItemsList,是如下,如何到一個ArrayList傳遞到AlertDialog片段

public class CustomDayRepeatFragment extends DialogFragment { 

    public interface DayRepeatListner { 
     void onFinishDayRepeatListner(List<String> days); 
    } 
    //List to save the result 
    private ArrayList<String> selectedDays; 
    String weekdays[]; 
    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     selectedDays = new ArrayList<>(); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     weekdays = getResources().getStringArray(R.array.weekdays); 
     Log.d("In dialog", "Custom"); 
     builder.setTitle("Select Days to Repeat") 
       .setMultiChoiceItems(weekdays, null, new DialogInterface.OnMultiChoiceClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
         if(isChecked) { 
          selectedDays.add(weekdays[which]); 
         } else if (selectedDays.contains(weekdays[which])) { 
          selectedDays.remove(String.valueOf(weekdays[which])); 
         } 
        } 
       }) 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         for(int i = 0; i < selectedDays.size(); i++) { 
          Log.d("Selected Days", selectedDays.get(i)); 
         } 

        } 
       }); 

     return builder.create(); 
    } 
} 

而且我從我的主要活動如下

CustomDayRepeatFragment dialog = new CustomDayRepeatFragment(); 
dialog.show(getSupportFragmentManager(), DIALOG_REPEAT_SETTING); 

叫它但是,我想發送一個List<string>到這個AlertDialog,然後利用這個列表。

我無法弄清楚如何實現它。

感謝所有幫助

+0

爲什麼不使用只是一個標準的制定者? – 2016-12-29 16:55:05

+0

當你說發送一個清單你的意思是從alertDialog發送到您的活動權利? – 2016-12-29 16:56:27

+0

@JawadLeWywadi從活動到alertDialog – Jagrati

回答

1

您可以在​​類中創建一個靜態的newInstance方法,然後按照以下方法從MainActivity調用它。在這裏,您可以在​​類的newInstance() method作爲參數發送列表現在

CustomDayRepeatFragment dialog = CustomDayRepeatFragment.newInstance(list); 
dialog.show(getSupportFragmentManager(), DIALOG_REPEAT_SETTING); 

可以作爲參數在Bundle列表保存爲bundle.putParcelableArrayList("list", list);,然後從包中自變量onCreateDialog() methodList<String> list = getArguments().getParcelableArrayList("list");檢索

您的newInstance和你onCreateDialog方法看起來就像

@NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     selectedDays = new ArrayList<>(); 


     List<String> list = (List<String>) getArguments().getStringArrayList("list"); // the list you pass as an argument 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     weekdays = getResources().getStringArray(R.array.weekdays); 
     Log.d("In dialog", "Custom"); 
     builder.setTitle("Select Days to Repeat") 
       .setMultiChoiceItems(weekdays, null, new DialogInterface.OnMultiChoiceClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
         if(isChecked) { 
          selectedDays.add(weekdays[which]); 
         } else if (selectedDays.contains(weekdays[which])) { 
          selectedDays.remove(String.valueOf(weekdays[which])); 
         } 
        } 
       }) 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 


         for(int i = 0; i < selectedDays.size(); i++) { 
          Log.d("Selected Days", selectedDays.get(i)); 
         } 

        } 
       }); 

     return builder.create(); 
    } 

    //Creating a fragment instance class to pass the parameters 
    public static CustomDayRepeatFragment newInstance(int groupPosition) { 
     CustomDayRepeatFragment fragment = new CustomDayRepeatFragment(); 

     Bundle bundle = new Bundle(); 
     bundle.putStringArrayList("list", (ArrayList<String>)list); 

     fragment.setArguments(bundle); 

     return fragment; 
    } 
+0

我會盡量讓你知道 – Jagrati

+0

我得到一個錯誤轉換列表ArrayList的 – Jagrati

+0

編輯的代碼,你需要強制轉換爲ArrayList的同時節省並列出而retriving –

相關問題