0

我有一個擴展了我的主要活動中的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(); 
    } 
} 

回答

0

我重新編排了程序的邏輯,現在它工作。不要在警報對話框類中聲明廣播接收器,而要使用主要活動的onCreate來調用它。這樣,您可以將字符串數組傳遞給對話框片段,然後使用該數組填充微調器。