0

I have my views built and everything working perfectly, except when i go to update the displayed textview.

I am using the windowmanager.addView() method to inflate a view flipper. one of the views in the fliper contains a textview. I would like to update this textview, though it seems to be impossible.

public void updateTextView(String inString) {
    TextView t = (TextView) findViewById(R.id.status);
    t.setText(inString);

    Log.d("INCOMING String IS FROM THIS!",
            inString + " : " + t.getText());
    flippy.setDisplayedChild(1);

}

the log message shows that the textview has the correct value, but the actual view itself still shows the default string coded in the xml.

Here are a few things I have tried.

postInvalidate(); invalidate(); post with a new runnable that calls invalidate. WindowManager.updateViewLayouts();

I'm at a bit of a loss, any ideas on how I can get this textview to display my values?

r2DoesInc
  • 3,759
  • 3
  • 29
  • 60
  • 1
    Are you sure that's the right `TextView`? Do you have more than one `TextView` with the id `status`? – kabuko Jul 10 '13 at 18:34
  • its the only on the page with an id. – r2DoesInc Jul 10 '13 at 18:34
  • OK, but you're using `findViewById` from the `Activity` and not the `View` right? Try getting the root view of the subhierarchy in question and doing `view.findViewById` instead. – kabuko Jul 10 '13 at 18:36
  • This is in the View class that its being called from. I could call this.findViewById, but thats redundant. – r2DoesInc Jul 10 '13 at 18:37
  • And all the other views and their listeners work fine, so the issue is not FINDING or setting the views, I just cant gett he view updated afterwards. – r2DoesInc Jul 10 '13 at 18:37

1 Answers1

1

You can do something like this:

WeakReference<ServiceClass> weakServ = new WeakReference<>(this);
volatile Handler uiH;
volatile TextView your_text_view;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    uiH = new Handler();
    ...
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mainView = inflater.inflate(R.layout.activity_main, null);
    wm.addView(mainView)
    your_text_view = mainView.findViewById(R.id.your_text_view_id);
    your_text_view.setText("original text");
    ...
    yourUpdateTask();
}

void updateText(text) {
    //simulate runOnUiThread of activity to solved the problem
    uiH.post(new Runnable() { 
        @Override
        public void run() {
            your_text_view.setText(text);
        }
    });
}

void yourUpdateTask() {
    //timer is just an example to reproduce the problem
    final Timer t = new Timer();
    final TimerTask tt = new TimerTask() { 
        @Override
        public void run() { //has problem to update your_text_view with new string
               ...
               ServiceClass yourServ = weakServ.get();
               if (yourServ!= null) {
                   yourServ.updateText(new_text);
               }
           }
        }
    };
    t.scheduleAtFixedRate(tt, 0, 1000); //run every 1 second
}
林果皞
  • 7,539
  • 3
  • 55
  • 70