Use Observer in Fragment

I have a problem when using an observer in a fragment, I have implemented my own TextViewObserver and I will not insert it into the fragment:

public class TextViewObserver extends TextView implements Observer {

public TextViewObserver(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public TextViewObserver(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TextViewObserver(Context context) {
    super(context);
}

@Override
public void update(java.util.Observable o, Object arg) {
    // TODO Auto-generated method stub
    this.setText(String.valueOf(arg));

}

      

My code snippet:

public class MyFragment extends Fragment {
private TextViewObserver mTextView;// = new TextView(this.getActivity());
private ApplicationContext mContext;
private DataObservable mDataObservable;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_6, container, false);
    mTextView = (TextViewObserver) view.findViewById(R.id.sicthtxtview);
    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = (ApplicationContext) activity.getApplicationContext();
    mDataObservable = mContext.getObservable();

}

@Override
public void onStart() {
    super.onStart();
    mDataObservable.addObserver(mTextView);
}

      

My DataObservable is in my ApplicationContext (Application Extension) class:

public class ApplicationContext extends Application {

private DataObservable _mData;

@Override
public void onCreate() {
    super.onCreate();
    _mData = new DataObservable();
}

public DataObservable getObservable() {
    return _mData;
}

      

MyTextViewObserver should be notified when the DataObservable changes and as a result of setText in the fragment. This is the error given by LogCat:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

      

thank

+3


source to share


1 answer


On Android, you can only control View

from the UI thread. You can run some code on the UI thread using View.post()

for example:

@Override
public void update(java.util.Observable o, final Object arg) {
    this.post(new Runnable() {
        public void run() {
            this.setText(String.valueOf(arg));
        } 
    }
}

      



http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)

+3


source







All Articles