当我需要时,Android不会更改我的TextView

时间:2014-03-28 10:34:32

标签: android eclipse

我需要更改文本视图,然后应用程序应该启动其他进程,但是会发生什么,然后进行工作并最后更改文本。

我很感激任何建议!

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

我试过了

  • 将设置的文本代码放入" work"代码。
  • 在继续
  • 之前,执行if语句以检查文本视图是否已更新

更多信息

  • 在"工作"有代码获取当前位置并通过电子邮件发送。
  • 当用户按下按钮时,我需要在gps和电子邮件工作开始之前更改文本。

完整代码

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@EMAIL", "); 

             String[] toArr = {"EMAIL@EMAIL"};      

             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@EMAIL"); 
             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()方法在电子邮件发送后不会结束。

4 个答案:

答案 0 :(得分: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
    }
                                            } 
            });

答案 1 :(得分: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
        }  

    }

答案 2 :(得分:0)

尝试在setText()方法中调用invalidate()方法。

答案 3 :(得分: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();
    }