RunOnUIThread from CustomCursorAdapter
I know this is late, but I hope this approach helps other people.
This solution works if I have Adapter (RecyclerView Adapter,Viewpager Adapter... etc)
, and in this adapter I'm working with AsyncTask
and want to change some data by using that AsyncTask
:
((Activity)contextObject).runOnUiThread(new Runnable() {
@Override
public void run() {
//change View Data
}
});
You need to somehow pass a reference of your activity to your adapter. then through some casting or use of interfaces you can call on ((YourActivity)context).runOnUiThread()
as for accessing the fragment manager, you need to do the same casting.
((YourActivity)context).getFragmentManager()
But be aware! this is not a good design pattern. if the context
is no an activity, you will soon have to deal with lots of NPE exceptions and stuff. For example, ApplicationContext
does not have getFragmentManager
but when you do the casting above, your IDE will not notify you, you will find that out when you actually try it on emulator or device.
Using interfaces are a bit safer, at least you know that your activity has which interface implemented.