FireDragon
FireDragon

Reputation: 3

Android-Threading: refer to Views from Threads

I've tried to use threads for better performance in my Android App. In the onCreate Method i started two threads:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play);

    ...

    new Thread(new Runnable() {
        public void run() {
         makeList();
        }
    }).start();

    new Thread(new Runnable() {
        public void run() {
         setChronometer();
        }
    }).start();  
}  

In these Methods I change the some settings of my Views. (no heavy events)

First a Chronometer:

 private void setChronometer(){
 time.post(new Runnable() {
         public void run() {
       time.setFormat("Time: %s");
       time.setBase(SystemClock.elapsedRealtime());
       time.start();
         }
       });
 }

Second a ListView:

private void makeList(){
  ...
        final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(...);  

        list.post(new Runnable() {
         public void run() {
          list.setAdapter(listAdapter); 
          list.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent,  View view, int position, long id) {
    ....

         }
       });
 }

The Chronometer works fine, but the operations in makeList() weren't done.. so my ListView isn't working!

I had the idea,that when the activity starts, defining the listView and Chronometer is less important, so i defer them in threads, with the aim, that the activity starts faster (is more performat)

First of all, was this a good Idea?

Secondly, why is my ListView not working? When i remove the list.post(new Runnable() { it works, but i thought that wasn't a good way in android. Cause now the Views are directy changed from a thread.

Thirdly, when should i use Threads vs.View.post?

Any help would be great, Thanks!

Greetings FireDragon

Upvotes: 0

Views: 292

Answers (2)

Michael De Silva
Michael De Silva

Reputation: 3818

Reuben is right about AsyncTask. However, if you want to understand how Threading works I've detailed the various approaches on blog. AsyncTask is the way to go though.

Upvotes: 0

Reuben Scratton
Reuben Scratton

Reputation: 38707

The first thing to remember is that the elements of your UI - your Views and ListViews etc - must only ever be accessed by your UI thread (i.e. your main thread). If you want to modify your UI from a background thread, you need to instantiate a Handler (on your UI thread) and send it messages and/or runnables from your background thread.

Sounds a bit messy? Fortunately Android includes a very powerful class called AsyncTask which is exactly what you need to get busy learning. You put your background thread code in it's doInBackground() member, and you do your UI updating in onPostExecute(), and it's oh-so-much simpler than fiddling around with new Thread() and Handlers etc.

Upvotes: 1

Related Questions