如何动态更改TextView?

时间:2018-05-31 15:22:24

标签: android textview ion

在MainActivity中调用所有方法。 在我的应用程序中,我从离子库获取一个HTML页面,我有一些方法来格式化我用离子下载的文本。

我正在尝试做的是在TextView中设置变量DoveSono,实际上在变量循环的每个循环中变量,所以TextView也应该改变,但实际上我已经完成了应用程序崩溃。

这是我通过获取HTML网站然后调用其他方法调用onClick和start ion的方法:

       private void getHTMLArticoli(){

   //     progressDialog = new SpotsDialog(articoli.this, R.style.Custom);
   //     progressDialog.show();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                htmlresultart = null;
                try {
                    htmlresultart = Ion.with(getApplicationContext())
                            .load("WEBSITEIP")
                            .asString()
                            .get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                if (htmlresultart != null) {
                    htmlresultart = htmlresultart.replace("</td>", "\n");
                    getTableArticoli();
                    getTestataArticoli();
                    getBodyArticoli();

      //              progressDialog.cancel();
                }

虽然这里是我在getHTMLArticoli中调用的getBodyArticoli方法,但你可以找到我试图设置文本的部分

    private void getBodyArticoli(){

        DaDoveParto = Integer.valueOf(String.valueOf(htmlresultart.indexOf("TBLCRP")));
        DoveMiFermo = Integer.valueOf(String.valueOf(htmlresultart.indexOf("</form>")));

        if(DaDoveParto == 0){
            Toast.makeText(this,"NESSUN DATO TROVATO",Toast.LENGTH_SHORT).show();
        }else
        {
            Integer i;
            Integer j;
            Integer CONTACAMPO = 0;
            for( i = DaDoveParto ; i <= DoveMiFermo ; i++){
                if( htmlresultart.substring(i, i + 4).equals("<td>")){
                    i += 4;
                    for (j = i; j <= DoveMiFermo ; j++){
                        if(htmlresultart.substring(j, j + 1).equals("\n")){

                            appBODYart[CONTACAMPO] = htmlresultart.substring(i, i + (j - i));

                            if(appBODYart[CONTACAMPO].equals("(null)")){
                                appBODYart[CONTACAMPO] = "";
                            }
                            CONTACAMPO += 1;

                            if(CONTACAMPO.equals(QuantiCampi)){
                                CONTACAMPO = 0;
                                myDB.insertArtServer(appBODYart[0], appBODYart[1], appBODYart[2], appBODYart[3], appBODYart[4], appBODYart[5],
                                        appBODYart[6], appBODYart[7], appBODYart[8]);

                                DoveSono +=0;
                                textView.setText(String.valueOf(DoveSono);
                            }
                            break;

实际上,当我尝试这样做时,我收到以下错误:

  

05-31 17:24:41.515 4605-4769 / com.example.igardini.visualposmobile   E / AndroidRuntime:FATAL EXCEPTION:pool-2-thread-1       处理:com.example.igardini.visualposmobile,PID:4605       android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。           在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363)           在android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)           在android.view.View.requestLayout(View.java:17484)           在android.view.View.requestLayout(View.java:17484)           在android.view.View.requestLayout(View.java:17484)           在android.view.View.requestLayout(View.java:17484)           在android.view.View.requestLayout(View.java:17484)           在android.view.View.requestLayout(View.java:17484)           在android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3112)           在android.view.View.requestLayout(View.java:17484)           在android.widget.TextView.checkForRelayout(TextView.java:6932)           在android.widget.TextView.setText(TextView.java:4083)           在android.widget.TextView.setText(TextView.java:3941)           在android.widget.TextView.setText(TextView.java:3916)           在com.example.igardini.visualposmobile.articoli.getBodyArticoli(articoli.java:121)           在com.example.igardini.visualposmobile.articoli.access $ 300(articoli.java:17)           在com.example.igardini.visualposmobile.articoli $ 2.run(articoli.java:79)           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)           在java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

我只需要包装

textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));

                                    runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {

                                        textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));

                                    }
                                });