我正在开发一个涉及搜索Wi-Fi直接同行的应用程序。当我按扫描两次重复出现。这是主要活动中对等点发现的代码
private void discoverService(){
DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
/* Callback includes:
* fullDomain: full domain name: e.g "printer._ipp._tcp.local."
* record: TXT record dta as a map of key/value pairs.
* device: The device running the advertised service.
*/
@Override
public void onDnsSdTxtRecordAvailable(
// this was modified to satisfy compiler if it causes crashes just remove the data type it takes
String fullDomain, Map<String, String> record, WifiP2pDevice device) {
Log.d("discoverService Error", "DnsSdTxtRecord available -" + record.toString());
buddies.put(device.deviceAddress,(String) record.get("myName"));
buddies.put("ipAddress", (String) record.get("ipAddress"));
}
};
DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
@Override
public void onDnsSdServiceAvailable(String instanceName, String registrationType,
WifiP2pDevice resourceType) {
// Update the device name with the human-friendly version from
// the DnsTxtRecord, assuming one arrived.
resourceType.deviceName = buddies
.containsKey(resourceType.deviceAddress) ? buddies
.get(resourceType.deviceAddress) : resourceType.deviceName;
// add to arraylist
// phones.add(resourceType);
// Add to the custom adapter defined specifically for showing
// wifi devices.
// add listAdapter
if (instanceName.equals("BeatsBox")){
devices.addToArrayList(resourceType);
appendStatus(resourceType.deviceAddress+ resourceType.deviceName);
}
Log.d("servListenter", "onBonjourServiceAvailable " + instanceName);
}
};
mManager.setDnsSdResponseListeners(mChannel, servListener, txtListener);
WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
mManager.addServiceRequest(mChannel,
serviceRequest,
new ActionListener() {
@Override
public void onSuccess() {
// Success!
}
@Override
public void onFailure(int code) {
// Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
}
});
mManager.discoverServices(mChannel, new ActionListener() {
@Override
public void onSuccess() {
appendStatus("Service discovery initiated");
}
@Override
public void onFailure(int arg0) {
appendStatus("Service discovery failed");
}
});
}
这是列表片段的代码
public class PeersFragment extends ListFragment {
private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
WiFiPeerListAdapter listAdapter = null;
public void addToArrayList(WifiP2pDevice device){
peers.add(device);
listAdapter.notifyDataSetChanged();
}
public void clearList(){
peers.clear();
listAdapter.clear();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listAdapter = new WiFiPeerListAdapter(this.getActivity(),
android.R.layout.simple_list_item_2, peers);
setListAdapter(listAdapter);
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_buttons_frag, container, false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
((StartScreen) getActivity()).connect(peers.get(position));
}
public class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
private List<WifiP2pDevice> items;
// * @param context
// * @param textViewResourceId
// * @param objects
public WiFiPeerListAdapter(Context context, int textViewResourceId,
List<WifiP2pDevice> objects) {
super(context, textViewResourceId, objects);
items = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.peers_fragment, null);
}
WifiP2pDevice device = items.get(position);
if (device != null) {
TextView top = (TextView) v.findViewById(R.id.device_name);
TextView bottom = (TextView) v.findViewById(R.id.device_address);
if (top != null) {
top.setText(device.deviceName);
}
if (bottom != null) {
bottom.setText(device.deviceAddress);
}
}
return v;
}
}
}
我正在查看日志,似乎发现Wi-Fi直接电话响应了几次 06-30 18:41:54.348:D / WifiP2pManager(24782):忽略{what = 139313 when = -17ms} 06-30 18:41:58.268:D / discoverService错误(24782):DnsSdTxtRecord可用 - {myName = Karim} 06-30 18:41:58.283:D / servListenter(24782):onBonjourServiceAvailable BeatsBox 06-30 18:41:58.298:D / AbsListView(24782):获取MotionRecognitionManager 06-30 18:41:58.328:D / discoverService错误(24782):DnsSdTxtRecord可用 - {myName = Karim} 06-30 18:41:58.348:D / servListenter(24782):onBonjourServiceAvailable BeatsBox 06-30 18:41:58.358:D / discoverService错误(24782):DnsSdTxtRecord可用 - {myName = Karim} 06-30 18:41:58.368:D / servListenter(24782):onBonjourServiceAvailable BeatsBox 当我第三次按下扫描时发生了这种情况。它回复了3次。似乎手机响应的次数与我按扫描的次数成正比。如果我没有任何循环,这怎么可能
答案 0 :(得分:0)
我认为你的适配器包含再次加载前的总数据,,,
在列表中加载适配器之前,您可以使用clearList()
方法吗?
OR:在加载之前清除适配器,如:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Here clear the adapter if it contains simething
if(listAdapter != null){
listAdapter.clear();
}
listAdapter = new WiFiPeerListAdapter(this.getActivity(),
android.R.layout.simple_list_item_2, peers);
setListAdapter(listAdapter);
}
检查一次
答案 1 :(得分:0)
您应该使用ViewHolder模式有效地缓存数据:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.peers_fragment, null);
ViewHolder holder = new ViewHolder();
holder.top = (TextView) v.findViewById(R.id.device_name);
holder.bottom = (TextView) v.findViewById(R.id.device_address);
v.setTag(holder);
}
WifiP2pDevice device = items.get(position);
if (device != null) {
holder.top.setText(device.deviceName);
holder.bottom.setText(device.deviceAddress);
}
return v;
}
static class ViewHolder {
TextView top;
TextView bottom;
}