Cannot resolve method .runOnUiThread
I have the same problem, and I replaced runOnUiThread()
with getActivity().runOnUiThread()
. And it worked.
You can move service handler as private member class inside your activity like below to use Activity's this context.
class MyActivity extends Activity {
private class ServiceHandler /** Whichever class you extend */ {
public void run() {
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
}
private MyListAdapterTracks mAdapter;
private ServiceHandler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Some code
mAdapter = new MyListAdapterTracks(getActivity(), R.layout.item_layout, list);
listView.setAdapter(mAdapter);
// Some code
ServiceHandler sh = new ServiceHandler();
sh.run();
}
}
EDIT:
public class ServiceHandler /** Whichever class you extend */ {
private final Activity mActivity;
private final MyListAdapterTracks mAdapter;
public ServiceHandler(Activity activity, MyListAdapterTracks adapter) {
mActivity = activity;
mAdapter = adapter;
}
@Override
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
// More code
}
And then instanciate it in your activity like this:
class MyActivity extends Activity {
private MyListAdapterTracks mAdapter;
private ServiceHandler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Some code
ServiceHandler sh = new ServiceHandler(this);
sh.run();
}
}