Search code examples
javaandroidsortingwifi

Java ArrayList Contains() method not working


I have no doubt that the contains() method is working correctly but apparently I am doing something wrong. I am working on an android app and I am detecting WiFi and listing the WiFi in a listView. 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):

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):

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!


Solution

  • .contains doesnt work, because you call List<String> SSIDs = new ArrayList<>(); within for(int i=0; i < apList.size(); i++){. You basically create new empty List for every loop-iteration. You should call it once befor the outer for-loop:

    List<String> SSIDs = new ArrayList<>();
    for(int i=0; i < apList.size(); i++){
        ...
    }