Skip to content →

Month: May 2015

Android Sort ScanResult by Signal Strength

// Get List of ScanResults
List<ScanResult> wifiList = wifiManager.getScanResults();

// Create Temporary HashMap
HashMap<String, ScanResult> map = 
  new HashMap<String, ScanResult>();

// Add ScanResults to Map to remove duplicates
for (ScanResult scanResult : wifiList) {
  if (scanResult.SSID != null && 
     !scanResult.SSID.isEmpty()) {
    map.put(scanResult.SSID, scanResult);
  }
}

// Add to new List
List<ScanResult> sortedWifiList = 
  new ArrayList<ScanResult>(map.values());

// Create Comparator to sort by level
Comparator<ScanResult> comparator = 
  new Comparator<ScanResult>() {

  @Override
  public int compare(ScanResult lhs, ScanResult rhs) {
    return (lhs.level < rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
  }
};

// Apply Comparator and sort
Collections.sort(sortedWifiList, comparator);            
Comments closed

Remove Divider in an Android ListView

I’ve had to remove the divider in an Android ListView several times now, so here’s a quick reminder of myself.

This can be done in XML or in Java by setting the dividerHeight to 0 and the divider to null:

XML

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="0dp"
        android:divider="@null"/>

Java

ListView listView = 
(ListView)findViewById(R.id.listView);
listView.setDividerHeight(0);
listView.setDivider(null);
Comments closed