Cannot resolve .runOnUiThread method

I found this here how to use the runOnUiThreaed method, but I don't understand how to use it.

I am setting up a list adapter in my main action

  // Set a gobal reference to the list adapter and the list respectivly ListAdapter listAdapter; static ArrayList<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // some code adapter = new ListAdapter(getActivity(), R.layout.item_layout, list); listView.setAdapter(adapter); // some code } 

I have a list adapter that calls a service handler class

 public class ListAdapter { // some code ServiceHandler sh = new ServiceHandler(); sh.run(); } 

Here is the .run() method in the ServiceHandler class, where I updated the list and list adapter

 public void run(Adapter listAdapter, ArrayList<String> list){ // some code list[0] = "foo"; listAdapter.notifyDataSetChanged; } 

But I get this runtime error

Only the source stream that created the view hierarchy can touch its views.

So I am trying to solve the error with .runOnUiThread

So here is the .run() method in the ServiceHandler class, with runOnUiThread

 public void run(Adapter listAdapter, ArrayList<String> list){ // some code runOnUiThread(new Runnable() { @Override public void run() { list[0] = "foo"; listAdapter.notifyDataSetChanged; }); } 

But I get

Unable to resolve 'runOnUiThread (anonymous Java.lang.runnable)' method

+8
android listview
source share
1 answer

You can move the service handler to the private class inside your activity, as shown below, to use the Activity 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 activate it in your activity as follows:

 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(); } } 
+8
source share

All Articles