-1

I have an ArrayList of Textviews populated by an expandListAdpater, my goal is to change the values of the textViews, but for some reason it works only once. I tried different timers, tried to tie my handler to the ui thread, it never works. here is my code. Please help! Expandlistadapter…</p>

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.
4

2 回答 2

0

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

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

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

于 2016-01-11T19:55:35.557 回答
0

我经常遇到类似的问题。我尝试了所有不同的方法。现在我正在使用 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

于 2016-01-10T23:41:53.453 回答