UI未从onProgressUpdate更新

时间:2013-05-13 06:01:32

标签: java android xml android-layout

我正在编写以下代码:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        start();
    }
    public void start()
    {
        ScrollView device= (ScrollView) findViewById(R.id.test);
        device.removeAllViews();
        TableLayout deviceTable = new TableLayout(getApplicationContext());
        deviceTable.setId(951357);
        device.addView(deviceTable);

        deviceList = new ArrayList<DeviceInformation>();

        WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
        if(wifi != null)
        {
            WifiManager.MulticastLock lock = wifi.createMulticastLock("WifiDevices");
            lock.acquire();
        }
        WifiInfo wInfo = wifi.getConnectionInfo();
        MAC = wInfo.getMacAddress();
        sendMulticastFlyport = new Thread(new FlyportSender(MAC));
        sendMulticastFlyport.start();
        sendMulticastComputer = new Thread(new ComputerSender(MAC));
        sendMulticastComputer.start();
        sendMulticastRaspberryPi = new Thread(new RaspberrySender(MAC));
        sendMulticastRaspberryPi.start();
        new DeviceSearcher().execute();
    }
    private class DeviceSearcher extends AsyncTask<Void, DeviceInformation ,Void>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            TableLayout deviceTable = (TableLayout) findViewById(951357);
            TableRow tr = new TableRow(getApplicationContext());
            LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            tr.setLayoutParams(layout);
            TextView tv = new TextView(getApplicationContext());
            tv.setText("Searching");
            tv.setTextColor(Color.BLACK);
            tv.setVisibility(1);
            tr.addView(tv);
            deviceTable.addView(tr);

        }
        @Override
        protected Void doInBackground(Void... arg0)
        {
            publishProgress((DeviceInformation)null);
            new Thread()
            {
                public void run()
                {
                    MulticastSocket socketComputer=null;
                    try
                    {
                        socketComputer = new MulticastSocket(WifiConstants.COMPUTER_RECV_PORT);
                        socketComputer.joinGroup(InetAddress.getByName(WifiConstants.COMPUTER_NETWORK_ADDR));
                        socketComputer.setSoTimeout(1*20*1000);
                        byte[] inBufComputer = new byte[1024];
                        DatagramPacket inPacketComputer = new DatagramPacket(inBufComputer, inBufComputer.length);

                        while(true)
                        {
                            System.out.println("Listening...");
                            socketComputer.receive(inPacketComputer);
                            System.out.println("Received");
                            String msg = new String(inBufComputer, 0, inPacketComputer.getLength());
                            DeviceInformation device = new DeviceInformation(1, msg, inPacketComputer.getAddress().toString());

                            publishProgress(device);

                            Log.v("Received:","Received Computer From :" + inPacketComputer.getAddress() + " Msg : " + msg);
                            //System.out.write(inPacket.getData(),0,inPacket.getLength());
                            System.out.println();
                            Thread.sleep(2000);
                        }
                    }
                    catch(Exception e)
                    {
                        Log.v("Exception:","During Receiving Computer: "+e.toString());
                        publishProgress((DeviceInformation)null);
                    }
                    finally
                    {
                        socketComputer.close();
                    }
                }
            }.start();

            new Thread()
            {
                public void run()
                {
                    MulticastSocket socketRaspberry=null;
                    try
                    {
                        socketRaspberry = new MulticastSocket(WifiConstants.RASPBERRY_RECV_PORT);
                        socketRaspberry.joinGroup(InetAddress.getByName(WifiConstants.RASPBERRY_NETWORK_ADDR));
                        socketRaspberry.setSoTimeout(1*20*1000);
                        byte[] inBufRaspberry = new byte[1024];
                        DatagramPacket inPacketRaspberry = new DatagramPacket(inBufRaspberry, inBufRaspberry.length);

                        while(true)
                        {
                            System.out.println("Listening...");
                            socketRaspberry.receive(inPacketRaspberry);
                            System.out.println("Received");
                            String msg = new String(inBufRaspberry, 0, inPacketRaspberry.getLength());
                            DeviceInformation device = new DeviceInformation(2, msg, inPacketRaspberry.getAddress().toString());

                            publishProgress(device);

                            Log.v("Received:","Received Raspberry From :" + inPacketRaspberry.getAddress() + " Msg : " + msg);
                            //System.out.write(inPacket.getData(),0,inPacket.getLength());
                            System.out.println();
                            Thread.sleep(1500);
                        }
                    }
                    catch(Exception e)
                    {
                        Log.v("Exception:","During Receiving Raspberry: "+e.toString());
                        publishProgress((DeviceInformation)null);
                    }
                    finally
                    {
                        socketRaspberry.close();
                    }
                }
            }.start();
        }

    @Override
    protected void onProgressUpdate(DeviceInformation... d)
    {
        super.onProgressUpdate(d[0]);
        LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        TableLayout deviceTable = (TableLayout) findViewById(951357);

        if(check==false)
        {
            check=true;
            Log.v("enter","removing all views");
            System.out.println(deviceTable);
            //deviceTable.removeAllViews();
        }
        DeviceInformation device = d[0];
        if(device!=null)
        {
            deviceList.add(device);
            TableRow tr = new TableRow(getApplicationContext());
            CheckBox check = new CheckBox(getApplicationContext());
            check.setText(device.getDeviceTypeString()+"\n"+device.getIPAddress());
            check.setVisibility(1);
            check.setId(14569+rowCounter);
            check.setTextColor(Color.WHITE);
            if(rowCounter%2==0)
            {
                tr.setBackgroundColor(Color.DKGRAY);
            }
            else
            {
                tr.setBackgroundColor(Color.GRAY);
            }
            check.setPadding(70, 0, 0, 0);

            check.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    CheckBox checkSee = (CheckBox) findViewById(view.getId());
                }
            });

            switch(device.getDeviceType())
            {
            case 1:
                check.setButtonDrawable(R.drawable.pcx);
                break;
            case 2:
                check.setButtonDrawable(R.drawable.raspberryx);
                break;
            case 3:
                check.setButtonDrawable(R.drawable.flyportx);
                break;
            }
            check.setEnabled(true);
            check.setChecked(false);
            check.setLayoutParams(layout);
            tr.addView(check);
            tr.setLayoutParams(layout);
            tr.setVisibility(1);
            deviceTable.addView(tr);
            rowCounter++;
        }
        else
        {
            if(deviceEndCounter==3)
            {
                TableRow tr = new TableRow(getApplicationContext());
                TextView tv = new TextView(getApplicationContext());
                tv.setText("No Devices Found");
                tv.setVisibility(1);
                tv.setLayoutParams(layout);
                tv.setTextColor(Color.WHITE);
                tr.setBackgroundColor(Color.BLACK);
                tr.setVisibility(1);
                tr.setLayoutParams(layout);
                tr.addView(tv);
                System.out.println(deviceTable);
                deviceTable.addView(tr);
            }
            else
            {
                deviceEndCounter++;
            }
        }
    }
}

上面的代码到达onProgressUpdate方法。并且还可以访问UI视图,但不要对UI进行任何更改。

但如果我要删除任何View,则会将其删除。但不添加任何视图。

我不知道该怎么做。我已经尝试了各种方法。

修改

我尝试使用TableLayout

打印用户界面System.out.println();
05-13 11:33:04.936: V/Exception:(16280): During Receiving Computer: java.net.SocketTimeoutException
05-13 11:33:04.946: V/Exception:(16280): During Receiving Raspberry: java.net.SocketTimeoutException
05-13 11:33:04.946: I/System.out(16280): android.widget.TableLayout@40dc32a8
05-13 11:33:04.956: V/Exception:(16280): During Receiving Flyport: java.net.SocketTimeoutException
05-13 11:33:04.976: V/Exception:(16280): During Receiving Computer: java.net.SocketTimeoutException
05-13 11:33:04.976: V/Exception:(16280): During Receiving Raspberry: java.net.SocketTimeoutException
05-13 11:33:04.986: V/Exception:(16280): During Receiving Flyport: java.net.SocketTimeoutException
05-13 11:33:05.016: I/System.out(16280): android.widget.TableLayout@40dc32a8

这是我的LogCat

除此之外,只有三个主题正在运行,但我不知道每个TimeOutException我是如何获得thread的。{/ p>

1 个答案:

答案 0 :(得分:0)

在上面,我正在使用

   LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
   TextView tv = new TextView(getApplicationContext());
   tv.setLayoutParams(layout);

它不应包含:

   tv.setLayoutParams(layout);