标题翻译
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<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;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论