2014-03-28 32 views
0

我需要文本視圖來更改,然後應用程序應該啓動其他進程,但會發生什麼,然後它會執行該操作並最後更改文本。當我需要它時,Android不會更改我的TextView

我將不勝感激任何建議!

public void onClick(View view) { 
    setText(); 
    work(); 
    } 

我已經試過

  • 把一套文本代碼中的 「工作」 的代碼。
  • 做的,如果檢查語句,如果文本視圖繼續

更多信息

  • 裏面的「工作」之前更新有用於獲取當前位置,並在電子郵件中發送它的代碼。
  • 當用戶按下按鈕時,我需要在gps和電子郵件工作開始之前更改文本。

Commplete代碼

public class VehicleInspection extends Activity { 

    private RadioGroup radioQ1Group; 
    private RadioButton radioQ1Button; 
    private Button btnDisplay; 
    EditText registration; 
    EditText odometer; 
    EditText comment; 
    CheckBox Check1; 
    CheckBox Check2; 
    CheckBox Check3; 
    CheckBox Check4; 
    double latitude; 
    double longitude; 
    int go = 0; 

    // GPSTracker Variables 

     GPSTracker gps; 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.activity_vehicle); 

     registration = (EditText) findViewById(R.id.editRegistration); 
     odometer = (EditText) findViewById(R.id.editOdometer); 
     comment = (EditText) findViewById(R.id.editComment); 

     SharedPreferences sharedPref= getSharedPreferences("mypref", 0); 
     String savedReg = sharedPref.getString("name", ""); 
     registration.setText(savedReg); 

     Check1 = (CheckBox)findViewById(R.id.checkBox1);  
     Check2 = (CheckBox)findViewById(R.id.checkBox2);  
     Check3 = (CheckBox)findViewById(R.id.checkBox3);  
     Check4 = (CheckBox)findViewById(R.id.checkBox4);  

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     Button addImage = (Button) findViewById(R.id.buttonSendVehicle); 
     addImage.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View view) { 
        setText(); 
        if (go==1) 
        { 
        work(); 
        } 
            } 
    }); 
    } 

     public void setText() 
     { 
      TextView tv = (TextView)findViewById(R.id.textComment); 
      tv.setText("IT WORKED!"); 
      go = 1; 
     } 

     public void work() 
     { 


      AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE); 
      Account[] list = manager.getAccounts(); 
      String gmail = null; 
      String rating = null; 
      String scheck1 = "FAIL"; 
      String scheck2 = "FAIL"; 
      String scheck3 = "FAIL"; 
      String scheck4 = "FAIL"; 
      Float ratingf = null; 

      if(Check1.isChecked()) 
       scheck1 = "PASS"; 
      if(Check2.isChecked()) 
       scheck2 = "PASS"; 
      if(Check3.isChecked()) 
       scheck3 = "PASS"; 
      if(Check4.isChecked()) 
       scheck4 = "PASS"; 

      for(Account account: list) 
      { 
       if(account.type.equalsIgnoreCase("com.google")) 
       { 
        gmail = account.name; 
        break; 
       } 
      } 

      gps = new GPSTracker(VehicleInspection.this); 
      Mail m = new Mail("[email protected]", "); 

      String[] toArr = {"[email protected]"};  

      String message = ("From : " + gmail + "\n" + "Registration : " + registration.getText().toString() + "\n" + "Odometer : " + odometer.getText().toString() + "\nComment : " + comment.getText().toString()); 
      message = message + "\nTires : " + scheck1 + "\nExterior : " + scheck2 + "\nWindows : " + scheck3 + "\nInterior : " + scheck4; 
      m.setFrom("[email protected]"); 
      m.setSubject("Yellolog : VI -" + " " + registration.getText().toString()); 
      m.setTo(toArr); 

      if(gps.canGetLocation()){ 

       latitude = gps.getLatitude(); 
       longitude = gps.getLongitude(); 

       message = message + "\nLocation : \nLat : " + latitude + "\nLong : " + longitude; 
       m.setBody(message); 

      }else{ 
      // can't get location 
      // GPS or Network is not enabled 
      // Ask user to enable GPS/network in settings 
      gps.showSettingsAlert(); 
     } 
     try { 
      // m.addAttachment("/sdcard/filelocation"); 

      if(m.send()|| latitude != 0.0) { 
       Toast.makeText(VehicleInspection.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 

       // Create object of SharedPreferences. 
       SharedPreferences sharedPref= getSharedPreferences("mypref", 0); 
      //now get Editor 
       SharedPreferences.Editor editor= sharedPref.edit(); 
      //put your value 
       editor.putString("name", registration.getText().toString());  
      //commits your edits 
       editor.commit(); 

       odometer.setText(""); 
       comment.setText(""); 
       Check1.setChecked(false); 
       Check2.setChecked(false); 
       Check3.setChecked(false); 
       Check4.setChecked(false); 
       TextView tv = (TextView)findViewById(R.id.textComment); 
       tv.setText("Comment"); 


      } else { 
       Toast.makeText(VehicleInspection.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
      } 
      } catch(Exception e) { 
      Toast.makeText(VehicleInspection.this, "There was a problem sending the email." + e, Toast.LENGTH_LONG).show(); 
      Log.e("MailApp", "Could not send email",e); 
      } 
     } 
    } 

進展!!

public void onClick(View view) { 

       tv.setText("IT WORKED!"); 

       if(tv.getText().toString().equals("IT WORKED!")){   
       runThread(); 
       }else{ 
       Toast.makeText(VehicleInspection.this, "Text Didnt change yet", Toast.LENGTH_LONG).show(); 

       } 
           } 
});   
    } 
    private void runThread() { 

     new Thread() { 
      public void run() { 
       while (i++ < 1000) { 
        try { 
         runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 

           work(); 

          } 
         }); 
         Thread.sleep(300); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }.start(); 
    } 

這將首先更改文本,併發送電子郵件,但work()方法在電子郵件發送後不會結束。

+1

在測試從「我曾嘗試過」部分所做的更改之前,您是否重建過? – Brovoker

+1

發佈您的完整代碼! –

+0

是的,我做了,它只是去完成所有的代碼工作,然後去設置文本。我需要先設置的文字 – user3472145

回答

0

您可以試試這種方法: -

 addImage.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 

          TextView tv = (TextView)findViewById(R.id.textComment); 
        tv.setText("IT WORKED!"); 

     if(tv.getText().toString().equals("IT WORKED!")){ 
Thread thread = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        Thread.sleep(1000); 
             work(); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     });thread.start(); 

     }else{ 
    //paste code for error 
    } 
              } 
      }); 
+0

謝謝!我馬上試試! – user3472145

+0

請接受我的答案,如果它的工作.. :) –

+0

它仍然只是去執行工作中的代碼。也許textview文本被更改,但它不會更新屏幕上的顯示或其他內容... – user3472145

0

你可以試試這個:

public class my_asyntask extends AsyncTask<String, String, String> { 
     TextView tv = (TextView)findViewById(R.id.textComment); 
     protected void onPreExecute() { 
     //you can set first here your textview 
     } 

     @Override 
     protected String doInBackground(String... params) { 
     //do work in background 
      return null; 
     } 


     protected void onPostExecute(String string) { 
     //finally settext your textview 
     } 

    } 
+0

感謝您的回答!它仍然是這樣。我覺得代碼正在工作,但有些東西阻止了顯示屏顯示更改,直到整個onclick完成。 – user3472145

+0

@ user3472145你可以檢查我的更新回答 –

+0

嗨!我試過但沒有運氣。當我點擊視圖時,它會凍結,完成所有代碼,然後應用程序解凍並對textView進行更改 – user3472145

0

嘗試調用裏面無效setText()方法()方法。

+1

嗨!我試過但沒有運氣。當我點擊視圖時,它凍結,完成所有代碼,然後應用程序解凍並對textView進行更改。 – user3472145

0

嘗試刪除此條件while (i++ < 1000)並在runOnUiThread之前使用Thread.sleep(1000)。嘗試是這樣的: -

private void runThread() { 


     new Thread() { 
      public void run() { 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 

          work(); 

         } 
        }); 
       } 

     }.start(); 
    } 
相關問題