为什么arraylist的大小通过此代码改变?

时间:2018-07-29 07:24:01

标签: android arraylist

我想在我的应用程序中搜索upnp设备,并且为它写了一些类,当我找到一个设备时,将其添加到一个名为sharedDevices的数组列表中。

sharedDevices在client.discoverServices中设置-> onServiceDiscovered在我的测试案例中,test3 textview中的大小为2,而test1 textview中的大小为0,为什么会这样?

client.discoverServices(networkStorageDevice, new DiscoveryListener() {
        @Override
        public void onServiceDiscovered(SsdpService service) {
            final SsdpService ssdpService = service;
                    device = "";
                    try {
                        urlConnectionReader = new URLConnectionReader(new URL(ssdpService.getLocation()));
                        String serviceType = ssdpService.getServiceType();
                        String fName = urlConnectionReader.getFriendlyName();
                        String ip = ssdpService.getRemoteIp().toString();
                        ip = ip.replaceAll("/","");
                        String port = Integer.toString(urlConnectionReader.getPort());
                        device =
                                "Service Type: " + serviceType + '\n' +
                                        "Friendly Name: " + fName + '\n' +
                                        "Remote IP: " + ip + '\n' +
                                        "Port Number: " + port + '\n';

                        service_ip = ip;
                        service_port = port;
                        sharedDevices.add(device);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                test3.setText(device);
                                //adapter.clear();
                                //devices.add(device);
                            }
                        });
                    } catch (MalformedURLException mue){
                        mue.printStackTrace();
                    }

        }
        @Override
        public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
            //System.out.println("Service announced something: " + announcement);
            // textView.setText("Service announced something: " + announcement.getLocation());
            final SsdpServiceAnnouncement ssdpServiceAnnouncement = announcement;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // textView.setText("Service announced something:\n " + ssdpServiceAnnouncement);
                    sharedDevices.add(ssdpServiceAnnouncement.toString());
                    adapter.notifyDataSetChanged();
                    textView.setText("");
                }
            });
        }
        @Override
        public void onFailed(Exception ex) {
            ex.printStackTrace();
        }
    });


    if(sharedPreferences != null) {
        ////////////test///////////////
        test0.setText("sharedPreferences != null");
        ///////////////////////////////
        if (sharedPreferences.contains(posKey)) {
            ////////////test///////////////
            test1.setText("sharedPreferences.contains(posKey)" + "sd_num = " + sharedDevices.size());
            ///////////////////////////////
            idx = -1;
            for (String device : sharedDevices) {
                test4.setText("device :" + device);
                if (device.equals(sharedPreferences.getString("posKey", ""))) {
                    idx = sharedDevices.indexOf(device);
                    ////////////test///////////////
                    test2.setText("device found");
                    ///////////////////////////////
                    break;
                }
            }
            if (idx != -1) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("");
                        String device = devices.get(idx);
                        devices.clear();
                        devices.add(device);
                        adapter.notifyDataSetChanged();
                    }
                });
            } 
        } 
    } 

0 个答案:

没有答案