首页 > 解决方案 > 如何动态更改我的 TextView?

问题描述

所有方法都在 MainActivity 中调用。在我的应用程序中,我从 ion 库中获取了一个 HTML 页面,并且我有一些方法可以格式化我用 ion 下载的文本。

我想要做的是DoveSono在 TextView 中设置变量,实际上在循环的每个循环中,变量都会改变,所以 TextView 也应该改变,但实际上我所做的应用程序崩溃了。

这是我调用 onClick 并从获取 HTML 网站开始的方法,然后它调用其他方法:

       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:致命异常: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:

标签: androidtextviewandroid-ion

解决方案


我只需要包装

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

                                    runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {

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

                                    }
                                });

推荐阅读