Android Studio:列表视图在Wifi p2p中不显示任何内容。

huangapple 未分类评论51阅读模式
标题翻译

Android Studio: listview shows nothing for Wifi p2p

问题

我正在编写用于发现WiFi并显示列表的代码。如果无法检测到WiFi,会弹出"Toast"消息:"无设备"。然而,列表和提示消息都没有显示出来。代码是否有问题?

我的代码如下:

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(),"无设备", Toast.LENGTH_SHORT).show();
        return;
    }
}
英文翻译

I'm writing code for discovering Wifi and showing the list. If wifi can't be detected, toasts "No Device". However, both list and toast are not showing up. Anything wrong with it?

My code is:

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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getApplicationContext(),android.R.layout.simple_list_item_1,deviceNameArray);
          listView.setAdapter(adapter);
        }

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

huangapple
  • 本文由 发表于 2020年5月30日 15:56:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/62099471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定