首页 > 解决方案 > Android Studio:listview 没有显示 Wifi p2p

问题描述

我正在编写用于发现 Wifi 并显示列表的代码。如果无法检测到 wifi,则敬酒“无设备”。但是,列表和吐司都没有出现。有什么问题吗?

我的代码是:

public void onPeersAvailable(WifiP2pDeviceList peerList) {
        if (!peerList.getDeviceList().equals(peers)){
          peers.clear();
          peers.addAll(peerList.getDeviceList());

          deviceNameArray=new String[peerList.getDeviceList().size()];
          deviceArray=new WifiP2pDevice[peerList.getDeviceList().size()];

          int index = 0;

          for(WifiP2pDevice device : peerList.getDeviceList()){
                deviceNameArray[index] = device.deviceName;
                deviceArray[index] = device;
                index++;
          }

          ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,deviceNameArray);
          listView.setAdapter(adapter);
        }

        if (peers.size()==0){
            Toast.makeText(getApplicationContext(),"No Device", Toast.LENGTH_SHORT).show();
            return;
        }
    }

标签: javaandroidlistviewwifip2p

解决方案


推荐阅读