我从 Android/JAVA 开发开始,我坚持使用 ListView 的 add 方法。我正在尝试创建一个包含所有可用 WiFi 网络的列表。当我在实习生类 WifiReceiver 中调用 add 方法时,什么也没有发生。我不明白。你能给我解释一下吗?
public class MainActivity extends Activity
{
TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
StringBuilder sb = new StringBuilder();
ListView list = null;
List<String> wifiData = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainText = (TextView) findViewById(R.id.mainText);
list = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, wifiData);
list.setAdapter(adapter);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
// mainText.setText("Starting Scan...");
}
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
mainWifi.startScan();
mainText.setText("Starting Scan");
return super.onMenuItemSelected(featureId, item);
}
protected void onPause()
{
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume()
{
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiReceiver extends BroadcastReceiver
{
public void onReceive(Context c, Intent intent)
{
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++)
wifiData.add(Integer.toString(i + 1) + "." + (wifiList.get(i)).toString());
wifiData.add("Bite");
}
}
}
谢谢你。