在警告对话框中填充微调器

时间:2014-06-20 19:52:39

标签: android broadcastreceiver android-spinner android-alertdialog android-dialogfragment

我有一个在我的主要活动中扩展dialogFragment的类。此类的目的是使用微调器和编辑文本打开一个对话框,然后将这些字符串返回到主活动。我在填充我的微调器时遇到了问题。微调器应该显示可用的wifi网络列表,但它没有显示任何内容。

所以我的问题是,在警报对话框中需要时,如何使用我的广播接收器类来填充微调器? 感谢您的帮助,以下是我目前的代码......

修改

我在我的程序中做了一些错误检查,似乎从对话框片段中永远不会调用广播接收器,如何让对话框片段中的广播接收器运行?

public static class MyAlertDialogFragment extends DialogFragment {
    Spinner sp;
    WifiManager mainWifiObj;
    WifiScanReceiver wifiReciever;
    ListView list;

    String wifis[];
    EditText nameANick;
    public static final String MyPREFERENCES = "MyPrefs";
    public static final String Name = "nameKey";
    SharedPreferences sharedpreferences;

    public static MyAlertDialogFragment newInstance(String title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.i("Dialog", "OnCreate");

        sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES,
                Context.MODE_PRIVATE);

        mainWifiObj = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        mainWifiObj.startScan();
    }

    public void onDestroy() {
        getActivity().unregisterReceiver(wifiReciever);
        super.onDestroy();

    }

    class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
            wifis = new String[wifiScanList.size()];
            int j = 0;

            if (sharedpreferences.contains(Name)) {
                wifis[0] = sharedpreferences.getString(Name, "");
                for (int i = 1; i < wifiScanList.size(); i++) {
                    wifis[i] = ((wifiScanList.get(j).SSID));
                    j++;
                }
            } else {
                for (int i = 0; i < wifiScanList.size(); i++) {
                    wifis[i] = ((wifiScanList.get(j).SSID));
                    j++;
                }

            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    getActivity(), android.R.layout.simple_spinner_item,
                    wifis);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sp.setAdapter(adapter);
        }
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        LinearLayout root = (LinearLayout) inflater.inflate(
                R.layout.my_dialog, null);

        nameANick = (EditText) root.findViewById(R.id.editText1);
        sp = (Spinner) root.findViewById(R.id.spinner1);


        return new AlertDialog.Builder(getActivity())
                .setTitle("Set Nickname1")
                .setView(root)
                .setPositiveButton("Save",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,

                                ((MainActivity) getActivity())
                                        .doPositiveClick();
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                ((MainActivity) getActivity())
                                        .doNegativeClick();
                            }
                        }).create();
    }
}

1 个答案:

答案 0 :(得分:0)

我重新安排了程序的逻辑,现在它可以工作了。不是在警报对话框类中声明广播接收器,而是使用主活动的onCreate调用它。这样,您可以将字符串数组传递给对话框片段,然后使用它填充微调器。