简体   繁体   English

在Android的ListView中对连接的WiFi进行排序

[英]Sorting connected wifi in listview in android

I am displaying available wifi in listview and I am connecting to wifi.. using dialog box in my android app..once wifi connected how to display that wifi name as first in listview..I want to display connected wifi as first in Listview..How can I do this? 我在列表视图中显示可用的wifi,并且正在连接到wifi ..使用我的Android应用程序中的对话框。.一旦wifi连接,如何在列表视图中首先显示该wifi名称。.我想在列表视图中首先显示已连接的wifi。 。我怎样才能做到这一点?

    TextView textView = (TextView)view.findViewById(R.id.Name);
     textView.setText(accessPoints.get(i);
     textView.setOnClickListener(
     new View.OnClickListener() {

        @Override
        public void onClick(View view) {
        final Dialog dialog =new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = dialog.getWindow();
        dialog.setContentView(R.layout.password_dialog);
        final Button connect = (Button)dialog.findViewById(R.id.connect);
        Button cancel = (Button)dialog.findViewById(R.id.cancel);
        connect.setOnClickListener(
        new View.OnClickListener() {

          @Override
           public void onClick(View view) {
           list<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
           for( WifiConfiguration i : list ) {
           if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
           wifiManager.disconnect();
           wifiManager.enableNetwork(i.networkId, true);
           wifiManager.reconnect();               
           break;
         }
        }
       }

The WifiManager class has a method getConnectionInfo() , which will return the WifiInfo object for the current connection. WifiManager类具有方法getConnectionInfo() ,它将为当前连接返回WifiInfo对象。 You can check the SSID String of this wifi and compare it with the WifiConfiguration list using a simple Java Comparator like this: 您可以使用以下简单的Java Comparator器检查此wifi的SSID String ,并将其与WifiConfiguration列表进行Comparator

public static class WifiComparator implements Comparator<WifiConfiguration> {

    WifiInfo connectedInto;

    public WifiComparator(WifiInfo connectedInto) {
        this.connectedInto = connectedInto;
    }

    @Override
    public int compare(WifiConfiguration lhs, WifiConfiguration rhs) {
        if (connectedInto == null)
            return 0;
        else {
            if (lhs.SSID.equals(connectedInto.getSSID()))
                return 1;
            return 0;
        }
    }
}

and then just sort the list by calling 然后通过调用将列表排序

Collections.sort(list, new WifiComparator(wifiManager.getConnectionInfo()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM