[英]Java ArrayList Contains() method not working
I have no doubt that the contains() method is working correctly but apparently I am doing something wrong. 我毫不怀疑contains()方法工作正常,但显然我做错了。 I am working on an android app and I am detecting WiFi and listing the WiFi in a listView. 我正在开发一个Android应用程序,我正在检测WiFi并在listView中列出WiFi。 I am attempting to filter out duplicate occurrences of the same Access Point so I am creating an array to keep track of duplicates. 我试图过滤掉同一个访问点的重复出现,所以我创建一个数组来跟踪重复项。 Here is my code: 这是我的代码:
public void getWifi(){
ListView wifiList = (ListView)findViewById(R.id.listView);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> apList = wifiManager.getScanResults();
wifiManager.startScan();
List<String> wifees = new ArrayList<>();
//split info into arrays
for(int i=0; i < apList.size(); i++){
String[] daList = String.valueOf(apList.get(i)).split(",");
String info = "";
List<String> SSIDs = new ArrayList<>();
for (String listItem : daList) {
String topic = String.valueOf(listItem.split(":")[0]);
String val = listItem.split(":")[1];
Log.d(TAG, topic);
if (Objects.equals(topic, "SSID")) {
if(SSIDs.contains(val)){
Log.d(TAG, "CONTAINS");
break;
} else {
SSIDs.add(val);
info = val;
}
} else if (Objects.equals(topic, " level")){
String strength = "";
if (Integer.parseInt(val.substring(1)) >= -35) {
strength = "100%";
} else if (Integer.parseInt(val.substring(1)) <= -35 && Integer.parseInt(val.substring(1)) >= -65) {
strength = "50%";
} else {
strength = "1%";
}
info += " " + strength;
}
}
wifees.add(info);
}
assert wifiList != null;
ArrayAdapter apter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, wifees);
wifiList.setAdapter(apter);
}
This is the contents of an apList item or what I get when I call apList.get(i): 这是apList项的内容或我调用apList.get(i)时得到的内容:
SSID: SCHOOL-GUEST, BSSID: 04:04:04:04:04:04, capabilities: [ESS], level: -91, frequency: 2412, timestamp: 72500687089, distance: ?(cm), distanceSd: ?(cm), passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, 80211mcResponder: is not supported
Here is the output of that code(roughly because it is on a phone and I would rather not include the AP names for security reasons): 这是该代码的输出(大致是因为它在电话上,出于安全原因我宁愿不包括AP名称):
SCHOOL-WIRELESS 100%
SCHOOL-SECURE 50%
SCHOOL-GUEST 50%
SCHOOL WIRELESS 1%
SCHOOL_WIRELESS 1%
SCHOOL-SECURE 1%
SCHOOL-GUEST 1%
bxz1872 50%
...
...
...
(so on and so forth)
If I could just get a second opinion that would be great. 如果我能得到第二个意见,这将是伟大的。 I feel like I am missing something obvious. 我觉得我错过了一些明显的东西。 Thanks! 谢谢!
.contains
doesnt work, because you call List<String> SSIDs = new ArrayList<>();
.contains
不起作用,因为你调用List<String> SSIDs = new ArrayList<>();
within for(int i=0; i < apList.size(); i++){
. 在for(int i=0; i < apList.size(); i++){
。 You basically create new empty List for every loop-iteration. 您基本上为每个循环迭代创建新的空List。 You should call it once befor the outer for-loop: 您应该在外部for循环中调用它一次:
List<String> SSIDs = new ArrayList<>();
for(int i=0; i < apList.size(); i++){
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.