Reputation: 1192
Here's the situation: I've got some lengthy non-UI code that needs to be run in a ListActivity and then have this ListActivity update the UI to contain a the result of this lengthy method (the list). I need a ProgressDialog to be running until it's finished so the user has some feedback.
Here's the code:
public class SolutionListActivity extends ListActivity
{
private String[] solutions;
private String letters;
private ProgressDialog dialog;
private static Solver solver;
/** Called when the activity is first created.
* @param savedInstanceState
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Get the selected letters from LettersActivity
letters = getIntent().getStringExtra("letters");
dialog = ProgressDialog.show(this, "Please Wait...",
"Searching Words...", true);
new Thread()
{
@Override
public void run()
{
if (solver == null)
{
solver = new Solver(SolutionListActivity.this);
solver.readDictionary(0);
solver.readDictionary(1);
}
solutions = solver.solve(letters);
runOnUiThread(new Runnable()
{
@Override
public void run()
{
dialog.dismiss();
//Set up a UI List
setListAdapter(new ArrayAdapter<String>(SolutionListActivity.this, R.layout.list_item, solutions));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
});
}
}.start();
The problem is my ProgressDialog
won't dismiss and I can't be sure whether the solutions = solver.solve(letters);
will be finished before the UI uses it in setListAdapter(new ArrayAdapter<String>(SolutionListActivity.this, R.layout.list_item, solutions));
Any advice you guys have would be helpful.
Thank You, Calum
Upvotes: 1
Views: 99
Reputation: 6461
Have you tried AsyncTask? It´s built exactly for having threading AND be able to update things in your UI Thread.
Take a look here: http://developer.android.com/resources/articles/painless-threading.html
Upvotes: 3