我有一个简单的wifi扫描应用程序。
我扫描wifis并将它们保存在List中。当我尝试添加ListView时,它始终为null,但在接收器类中我可以访问它们。如何在列表前填写List?这是我的代码:
注意:我先扫描然后创建片段。
WifiReceiver Class
class WifiReceiver : BroadcastReceiver{
public static List<ScanResult> wifiList;
public override async void OnReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
wifiList = wifiManager.ScanResults.ToList();
await Task.Delay(TimeSpan.FromSeconds(1));
wifiManager.StartScan();
}
}
WifiListFragment Class
public class WifiListFragment : ListFragment {
public override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
List<ScanResult> wifiList = WifiReceiver.wifiList.ToList();
List<string> eliminatedWifi = new List<string>();
//Some eliminations
ArrayAdapter<string> adapter = new ArrayAdapter<string>(Application.Context, Resource.Layout.WifiListRow, eliminatedWifi);
ListAdapter = adapter;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.Inflate(Resource.Layout.WifiListFragment, container, false);
return view;
}
public override void OnListItemClick(ListView l, View v, int position, long id) {
base.OnListItemClick(l, v, position, id);
}
}