1

I have a sort of numeric keypad and a list. When the user chooses the keypad, I call a function to sort the list. The user can press the button to show the list at any time.

But that list can be pretty large, 500+, so I was thinking about doing the sorting no longer on the UI thread.

What is the best way to do this ?

Should I use a regular thread, asynctask?

My only worry is that the user can also click the button to show the list while the asynctask is not finished yet. How should I handle that ?

Thx

slezadav
  • 6,104
  • 7
  • 40
  • 61
Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • use mutli-threading in android [see this post][1], i hope it helps [1]: http://stackoverflow.com/questions/5517197/how-to-create-multi-thread-in-android – Aha Oct 23 '12 at 11:58

5 Answers5

3

Definitely go for AsyncTask which is designed for heavy work outside UI. Concerning your last question, make button disabled and enable it in AsyncTask's onPostExecute(). Cheers.

slezadav
  • 6,104
  • 7
  • 40
  • 61
2

Definitely you need a asynctask. In your onCreate method hide all of your buttons and textviews. Before you do something in the background you need to show user a load bar or a spinner so that user can't click any of the buttons you have created. Here is a sample:

class LoadAllProducts extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(Academic.this);
         pDialog.setMessage("Loading. Please wait...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(false);
         pDialog.show();
}

protected String doInBackground(String... args) {
       //Do something
       return null;
}

protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all products
    pDialog.dismiss();
    // updating UI from Background Thread
    runOnUiThread(new Runnable() {
           public void run() {
             //finally show your button
          }
    });
}

Before using this code make sure you have declared the pDialog. In my program I had used spinner.

NewUser
  • 3,729
  • 10
  • 57
  • 79
1

AsyncTask looks good for this... task. You can also make some sort of state-member to check whether the sort task is running and depending on it start the task or do nothing when the user presses the button. As an alternative, if something has changed, you can also cancel the task and start a new one.

stan0
  • 11,549
  • 6
  • 42
  • 59
0

Go for Async Task and you can show some progress bar in onPreExecute() and stop it in onPostExecute(). User wont be able to clcik anything till progress bar is running on screen.

Pankaj Kushwaha
  • 300
  • 1
  • 13
0

You can use AsyncTask as other have told or you can create a thread and run long running operations and update UI accordingly with handlers. Have a look at this link http://www.vogella.com/articles/AndroidPerformance/article.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256