【问题标题】:runOnUiThread settext only works at the first time during a looprunOnUiThread settext 仅在循环期间的第一次工作
【发布时间】:2016-01-10 22:11:21
【问题描述】:

我有一个由 expandListAdpater 填充的 Textview 的 ArrayList,我的目标是更改 textView 的值,但由于某种原因它只能工作一次。我尝试了不同的计时器,试图将我的处理程序绑定到 ui 线程,但它永远无法工作。这是我的代码。请帮忙! Expandlistadapter…

TextView mytexts= ButterKnife.findById(view, R.id.mytexts);
mySubSections.add(new ConstructForKeepingTextviewsForUpdate(mytexts));
// I got about 10 more textviews , this is an example

public class ConstructForKeepingTextviewsForUpdate {
    private TextView getTextviewToBeUpdated() {
        return textviewToBeUpdated;
    }

    public void SetVal(String t){
        getTextviewToBeUpdated().setText(t);
    }


    TextView textviewToBeUpdated;

    public ConstructForKeepingTextviewsForUpdate(TextView textviewToBeUpdated) {
        this.textviewToBeUpdated = textviewToBeUpdated;
        }

}

in onCreate I run this

private void pleaseWork(){
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    updateNumbersInLoop();
                }
            });
        }
    }, 0, 1000);
}

public static void updateNumbersInLoop() {
    for (ConstructForKeepingTextviewsForUpdate subSec : mySubSections){
       String datext = dbHelper.getValue (computedValue);
       subSec.SetVal(datext); 
    }
}
//The getValue function works , I can see the correct value, but the settext works only once, at the first time.

【问题讨论】:

    标签: android multithreading settext android-runonuithread


    【解决方案1】:

    我经常遇到类似的问题。我尝试了所有不同的方法。现在我正在使用 AsynTasc 来避免这种情况。它有一个名为 onProgressUpdate() 的方法。它在 UI 线程上运行,在这个方法中你可以更新你的 TextViews。计算本身(例如你的循环)可以在 doInBackground() 方法中处理。此方法在自己的线程中运行。总是当你想更新你的 TextView 时,你会在 doInBackground() 方法中调用 publishProgress("YourText") 。然后参数将被传递到 onProgressUpdate() 更新您的 TextView 的位置。

        private class MultiplayerIntro extends AsyncTask<Void, String, Void> {
    
        @Override
        protected Void doInBackground(Void... result) {
            try{
            String text;
            for(...){
                text = ...;
                publishProgress(text);
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            // ...
        ]
            return null;
        }
    
        @Override
        protected void onProgressUpdate(String... progress) {
             yourTextView.setText(progress[0]);
        }
    
        @Override
        protected void onPostExecute(Void result) {
             // ...
        }
    }
    

    然后你开始任务:

    new MultiplayerIntro().execute();
    

    你可以在这里找到很多参数的一个很好的解释: Stackoverflow

    【讨论】:

      【解决方案2】:

      实际上,当我使用代码时,asyncTask 给出了相同的结果。显然,由于某种原因,您不能将 textview 作为对象传递。所以真正有效的是

      TextView myTextV=  (TextView) findViewById(ConstructForKeepingTextviewsForUpdate.getItsID());
      myTextV.setText("anything you like");
      

      您应该做的是将 id 作为整数传递。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2016-02-17
        • 2017-09-19
        • 1970-01-01
        • 2015-09-21
        相关资源
        最近更新 更多