package jp.gihyo.mitoma.android.s1201dialog.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import jp.gihyo.mitoma.android.s1201dialog.R;

public class MyDialog03Fragment extends DialogFragment {
    private String[] aryWeather = {"晴れ", "曇り", "雨", "雪"};
    private int selectItem = 0;
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle("今日の天気");
        builder.setSingleChoiceItems(aryWeather, selectItem,new MyDialogRadioListener());
        builder.setPositiveButton("送信",new MyDialogRadioListener());
        Dialog dialog = builder.create();
        return dialog;
    }
    private class MyDialogRadioListener implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int itemNo) {
            EditText et = requireActivity().findViewById(R.id.etDialogData);
            if(itemNo >= 0){
                selectItem = itemNo;
            } else{
                et.setText(aryWeather[selectItem]);
            }
        }
    }
}