-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…

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.
AndroidFan
  • 21
  • 5

2 Answers2

0

I had similar problems often. I tried all different ways. Now I am using AsynTasc to avoid this situation. It has a method called onProgressUpdate(). It runs on the UI Thread and inside this method you can update your TextViews. The calculation itselft (e.g. your loop) can be processed in the doInBackground() method. This method is running in a own Thread. Always when you want to update yor TextView you will call publishProgress("YourText") inside the doInBackground() method. Then the parameter will be passed to onProgressUpdate() where your TextView gets updated.

    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) {
         // ...
    }
}

Then you start the Task with:

new MultiplayerIntro().execute();

You can find a good explanation of the many Parameters here: Stackoverflow

Community
  • 1
  • 1
skymedium
  • 767
  • 7
  • 26
0

Actaully as I played with the code, asyncTask gave the same result. Apparently you can't pass textview as an object, for some reason. So what actually works is

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

what you should do is pass the id as integer.

AndroidFan
  • 21
  • 5