2013-05-13 69 views
0

更新我写这下面的代码:UI不是从onProgressUpdate

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进行任何更改。

但如果我想删除任何查看它正在被删除。但不添加任何视图。

我不知道该怎么办。我尝试了各种方法。

编辑:

我尝试使用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): [email protected] 
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): [email protected] 

这是我LogCat打印UI TableLayout

除此之外,只有三个线程正在运行,但我不知道如何得到TimeOutException不止一次每thread

+0

的[Android的UI不编程更新]可能重复(http://stackoverflow.com/questions/16510503/android-ui-not-updating-programatically) – Raghunandan 2013-05-13 06:17:59

+1

我想这只是我的问题..而那不是正确的答案..它不工作..你一定看过评论。 – 2013-05-13 06:58:30

回答

0

在上文中,我使用的

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

尽管它不应该包含:

tv.setLayoutParams(layout); 
相关问题