TextView setText() not work sometimes

427 views Asked by At

I use eventbus to update TextView but is not work sometime first, after i change with the second it works,but i don't know how it works. call you help me?Thanks very much.below is my code. first it's not work

 @Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(Event event) {
    final Event loca = event;
    System.out.println("---sta--" + "onEvent");
    if (!isVisible || !isLoad) {
        return;
    }
    System.out.println("---sta--" + "visonEvent" + loca.getAddress()+TextUtils.isEmpty(loca.getAddress()));
    txtLocationDetails.setText(String.format("address:%s", TextUtils.isEmpty(loca.getAddress()) ? "" : loca.getAddress()));
    offset = 0;
    initDatas();

}

but i change the code below it work

 @Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(Event event) {
    final Event loca = event;
    System.out.println("---sta--" + "onEvent");
    if (!isVisible || !isLoad) {
        return;
    }
    System.out.println("---sta--" + "visonEvent" + loca.getAddress()+TextUtils.isEmpty(loca.getAddress()));
    txtLocationDetails.post(new Runnable() {
        @Override
        public void run() {
            txtLocationDetails.setText(String.format("address:%s", TextUtils.isEmpty(loca.getAddress()) ? "" : loca.getAddress()));
        }
    });
    offset = 0;
    initDatas();

}

i don't know why it's ok,can you tell me ?Thanks .

1

There are 1 answers

3
Ashish Vora On

While you are using thread do not try to update data from it directly.

Else make a new method and call from thread to update data in TextView.

You can try this code to make changes in TextView text:

@Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEvent event) {
    textField.setText(event.message);
}