我正在构建一个应用程序,允许用户无需离开即可连接到本地的wifi网络。但是,每当我在列表中选择一个项目时,就会出现错误的网络ID,并且它连接到错误的网络。我注意到了:
以下是我的代码:
//from `onCreate` method
Button buttonScan = (Button) findViewById(R.id.buttonScan);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
connectToWifi(arg2);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
return true;
}
});
if (!wifi.isWifiEnabled()) {
Toast.makeText(getApplicationContext(), "Enabling Wifi", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
SimpleAdapter adapter =
new SimpleAdapter(
NetworkCalibration.this,
arraylist,
R.layout.wifi_list_row,
new String[] { ITEM_KEY },
new int[] { R.id.listValue });
lv.setAdapter(adapter);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
results = wifi.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Selecter(hNum, vNum);
buttonScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noob = false;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Newbie", noob);
editor.commit();
arraylist.clear();
wifi.startScan();
Toast.makeText(getApplicationContext(), "Scanning...", Toast.LENGTH_SHORT).show();
try {
size = size - 1;
while (size >= 0) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY,
results.get(size).SSID.toString()
+ results.get(size).capabilities.toString());
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
public void finallyConnect(String checkPassword, int position) {
String networkSSID = results.get(position).SSID;
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\""+ networkSSID +"\"";
wifiConfiguration.preSharedKey ="\""+ checkPassword +"\"";
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfiguration);
if (!wifiManager.isWifiEnabled()) { //---wifi is turned on---
//---disconnect it first---
wifiManager.setWifiEnabled(true);
}
wifiManager.enableNetwork(netId, true);
wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, networkSSID);
wifiManager.reconnect();
wifiManager.saveConfiguration();
/* WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
// remember id
int netId = wifi.addNetwork(wifiConfig);
wifi.disconnect();
wifi.enableNetwork(netId, true);
wifi.reconnect();
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"\"" + networkSSID + "\"\"";
conf.preSharedKey = "\"" + networkPass + "\"";
wifi.addNetwork(conf);*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Connected",isConnected);
editor.commit();
}
private void connectToWifi(final int position) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.wifi_connect);
dialog.setTitle("Connect to Network");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1);
TextView textBSSID = (TextView) dialog.findViewById(R.id.textBSSID1);
TextView capabilities = (TextView) dialog.findViewById(R.id.textCapabilities);
Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
pass = (EditText) dialog.findViewById(R.id.textPassword);
pass.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(pass, InputMethodManager.SHOW_IMPLICIT);
textSSID.setText(results.get(position).SSID);
textBSSID.setText(results.get(position).BSSID);
capabilities.setText(results.get(position).capabilities);
// if button is clicked, connect to the network;
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkPassword = pass.getText().toString();
finallyConnect(checkPassword, position);
statusText.setText("Connecting...");
statusText.setTextColor(Color.parseColor("#000000"));
CheckConnection();
dialog.dismiss();
}
});
dialog.show();
}
知道为什么会这样吗?谢谢大家!
答案 0 :(得分:5)
我通过在代码中反转一些符号并重新定义一些变量来修复此问题。下面的最终工作代码:
try {
size = 0;
while (size >= 0) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(
ITEM_KEY,
results.get(size).SSID.toString() + results.get(size).capabilities.toString()
);
arraylist.add(item);
size++;
adapter.notifyDataSetChanged();
} catch (Exception e) {
}
而且&#39;啦!
答案 1 :(得分:1)
将项目添加到适配器时,您将以相反的方式进行迭代。也就是说,您正在从results
获取最后一项并将其存储在arraylist
的第一位。我建议您只需通过更改此代码来反转当前循环:
try {
size = size - 1;
while (size >= 0) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(
ITEM_KEY,
results.get(size).SSID.toString() + results.get(size).capabilities.toString()
);
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
} catch (Exception e) {
}
这个:
try {
for (int i = 0; i < size; i++ ) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(
ITEM_KEY,
results.get(size).SSID.toString() + results.get(size).capabilities.toString()
);
arraylist.add(item);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
}