我如何动态更改我的 TextView?
How can i dynamically change my TextView?
所有方法都在MainActivity中调用。
在我的应用程序中,我从 ion 库中获得了一个 HTML 页面,并且我有一些方法可以格式化我用 ion 下载的文本。
我想要做的是在 TextView 中设置变量 DoveSono
,实际上在循环的每个循环中变量都会改变,所以 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: FATAL EXCEPTION: pool-2-thread-1
Process: com.example.igardini.visualposmobile, PID: 4605
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)
at android.view.View.requestLayout(View.java:17484)
at android.view.View.requestLayout(View.java:17484)
at android.view.View.requestLayout(View.java:17484)
at android.view.View.requestLayout(View.java:17484)
at android.view.View.requestLayout(View.java:17484)
at android.view.View.requestLayout(View.java:17484)
at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3112)
at android.view.View.requestLayout(View.java:17484)
at android.widget.TextView.checkForRelayout(TextView.java:6932)
at android.widget.TextView.setText(TextView.java:4083)
at android.widget.TextView.setText(TextView.java:3941)
at android.widget.TextView.setText(TextView.java:3916)
at com.example.igardini.visualposmobile.articoli.getBodyArticoli(articoli.java:121)
at com.example.igardini.visualposmobile.articoli.access0(articoli.java:17)
at com.example.igardini.visualposmobile.articoli.run(articoli.java:79)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
我只需要包装
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
和
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
}
});
所有方法都在MainActivity中调用。 在我的应用程序中,我从 ion 库中获得了一个 HTML 页面,并且我有一些方法可以格式化我用 ion 下载的文本。
我想要做的是在 TextView 中设置变量 DoveSono
,实际上在循环的每个循环中变量都会改变,所以 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: FATAL EXCEPTION: pool-2-thread-1 Process: com.example.igardini.visualposmobile, PID: 4605 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363) at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3112) at android.view.View.requestLayout(View.java:17484) at android.widget.TextView.checkForRelayout(TextView.java:6932) at android.widget.TextView.setText(TextView.java:4083) at android.widget.TextView.setText(TextView.java:3941) at android.widget.TextView.setText(TextView.java:3916) at com.example.igardini.visualposmobile.articoli.getBodyArticoli(articoli.java:121) at com.example.igardini.visualposmobile.articoli.access0(articoli.java:17) at com.example.igardini.visualposmobile.articoli.run(articoli.java:79) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
我只需要包装
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
和
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
}
});