2014-12-06 85 views
0

我正在創建一個應用程序,它從json中的服務器獲取homestatus,但是這發生在另一個線程上。當我嘗試在UI上設置大多數項時,這不是問題,因爲我可以將它們設置爲靜態無效。但是當我嘗試創建一個新的開關和空間時,我無法調用'this'來創建一個新的。來自其他線程的Android UI

用於獲取homestatus

代碼:

public void loadHomeStatus() 
{ 
    if(socket != null) {`enter code here` 
     if (socket.isConnected()) { 
      Log.d("BtnUpdate","already connected"); 
      return; 
     } 
    } 

    swAlarm = (Switch) findViewById(R.id.swAlarmState); 
    tvTemperature = (TextView) findViewById(R.id.tvTemprateur); 
    tvHumidity = (TextView) findViewById(R.id.tvHumidity); 
    llDevices = (LinearLayout) findViewById(R.id.llDevices); 

    new Thread() { 
     public void run() { 
      try 
      { 
       busyConnecting = true; 
       Log.d("loadHomeStatus","trying to connect to: " + host + ":" + port); 
       socket = new Socket(host, port); 
       uiConnected(); 

        Log.d("loadHomeStatus","Connected"); 
        DataOutputStream os = new DataOutputStream(socket.getOutputStream()); 
        DataInputStream is = new DataInputStream(socket.getInputStream()); 


        os.writeBytes(password); 
        Log.d("Connect", "send: " + password); 
       while (socket.isConnected()) { 
        byte[] data = new byte[500]; 
        int count = is.read(data); 
        String recieved = new String(data).trim(); 
        Log.d("loadHomeStatus","recieved " + recieved); 
        if(recieved.toLowerCase() == "failed") 
        { 
         Log.d("loadHomeStatus","failed to log in"); 
        } 
        else 
        { 
         try 
         { 
          homeStatus = new Gson().fromJson(recieved, HomeStatus.class); 
          uiLoadStatus(); 
         } catch (Exception e) 
         { 
          Log.d("Error", e.toString()); 
         } 
        } 
       }//end of while loop 

       Log.w("loadHomeStatus", "end connection thread "); 
       //ends thread 
       Thread.currentThread().interrupt(); 
       return; 
      } 
      catch (UnknownHostException e) 
      { 
       e.printStackTrace(); 
       Log.w("loadHomeStatus", "no Failed to connect: " + host + "-" + 8001); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       Log.w("loadHomeStatus", "no Failed to connect: " + host + "-" + 8001); 
      } 
      Log.w("loadHomeStatus","Connection ended"); 
      socket = null; 
      busyConnecting = false; 
      uiDisconnected(); 
     } 
    }.start(); 
}` 

代碼設置UI

public static void uiLoadStatus() 
{ 
    if (homeStatus != null) 
    { 
      try { 

       tvTemperature.post(new Runnable() 
       { 
        public void run() 
        { 
         //Log.d("uiLoadStatus to string",homeStatus.toString()); 

         tvTemperature.setText(homeStatus.temperature + "°C"); 
         tvHumidity.setText(homeStatus.humidity + "%"); 
        } 
       }); 
     } 
     catch(Exception e) 
     { 
      Log.d("uiLoadStatus status fragment", e.toString()); 
     } 


     try { 
      swAlarm.post(new Runnable() 
      { 
       public void run() { 
        swAlarm.setChecked(homeStatus.alarmState); 
       } 
      }); 
     } 
     catch (Exception e) 
     { 
      Log.d("uiLoadStatus alarm fragment", e.toString()); 

     } 
    } 
    try { 
     llDevices.post(new Runnable() 
     { 
      public void run() { 
       uiLoadDevices(); //this gives and error because it's not static 
      } 
     }); 
    } 
    catch (Exception e) 
    { 
     Log.d("uiLoadStatus alarm fragment", e.toString()); 

    } 
} 


public void uiLoadDevices() 
{ 
    for (int i = 0; i < homeStatus.lstDevices.size(); i++) { 

     String deviceAdd = homeStatus.lstDevices.get(i); 
     Space mySpace = new Space(this); 
     Switch mySwitch = new Switch(this); 

     mySpace.setMinimumHeight(50); 
     mySwitch.setText(homeStatus.getName(deviceAdd)); 
     mySwitch.setChecked(homeStatus.getState(deviceAdd)); 
     mySwitch.setTextSize(18); 

     LinearLayout.LayoutParams lp = new   LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,  LinearLayout.LayoutParams.WRAP_CONTENT); 
     lp.gravity = Gravity.LEFT; 

     llDevices.addView(mySpace, lp); 
     llDevices.addView(mySwitch, lp); 
    } 
} 
+0

你看看AsyncTask,它的三個原因是這個原因 – nPn 2014-12-06 15:17:08

回答

0

您應該使用AsyncTask並將網絡交互部分放在doInBackground()方法中。要更新UI組件,在onPostExecute()方法中實現這些邏輯

+0

我會試試這個。這似乎是更好的做法 – 2014-12-06 15:18:59

0

uiLoadStatus是一個靜態方法(不知道爲什麼,或者如果它必須不看所有的代碼),因此你不能調用其中的非靜態方法,如uiLoadDevices

我建議看看你的代碼並更新你的uiLoadStatus如果可能的話不要是靜態的。濫用靜態會導致代碼冗長。