以下是这种情况:我有一些冗长的非UI代码需要在ListActivity中运行,然后让这个ListActivity更新UI以包含这个冗长方法(列表)的结果。我需要一个ProgressDialog才能运行直到它完成,所以用户有一些反馈。
以下是代码:
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();
问题是我的ProgressDialog
不会被忽视,我无法确定在solutions = solver.solve(letters);
setListAdapter(new ArrayAdapter<String>(SolutionListActivity.this, R.layout.list_item, solutions));
你们的任何建议都会有所帮助。
谢谢你, Calum
答案 0 :(得分:3)
你试过AsyncTask吗?它的构建完全是为了具有线程并且能够更新UI线程中的内容。
看看这里:http://developer.android.com/resources/articles/painless-threading.html