Fel*_*v.- 1 multithreading android progressdialog android-listview
我从服务器获取一个 JSON,它是我的 ListView 的内容。一切都很好,但我想在调用 Json 时创建一个 ProgressDialog。问题是我需要在不同的线程中调用 JSON(使用 http 方法)并在 ListView 中向用户显示结果。
这就是我正在做的事情,但出现了错误"05-09 12:13:28.358: W/System.err(344): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."
try {
final ProgressDialog progDailog;
progDailog =ProgressDialog.show(this,"HI", "Loading");
new Thread(new Runnable() {
@Override
public void run() {
try
{
JSONArray json = new JSONArray(executeHttpGet("http://m-devsnbp.insightlabs.cl/cuentos/json"));
progDailog.cancel();
ArrayList<HashMap<String, String>> talesList = new ArrayList<HashMap<String, String>>();
talesList = jsonToHash(json);
adapter = new LazyAdapter((Activity) ctx, talesList, 2);
list.setAdapter(adapter);
}
catch (InterruptedException e)
{
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如何从不同的线程更新我的观点?或者这是一个更好的方法来做到这一点?
您需要在主 ui 线程上更新 ui。使用runOnUiThread
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
progDailog.cancel();
adapter = new LazyAdapter((Activity) ctx, talesList, 2);
list.setAdapter(adapter);
}
});
Run Code Online (Sandbox Code Playgroud)
还可以考虑使用 asynctask。我建议 ypu 使用 asynctask
http://developer.android.com/reference/android/os/AsyncTask.html
ProgressDialog pd;
LazyAdapter adapter;
ListView list;
ArrayList<HashMap<String, String>> talesList = new ArrayList<HashMap<String, String>>();
Run Code Online (Sandbox Code Playgroud)
在您的活动 onCreate() 中
pd = new ProgressDialog(ActivityName.this);
pd.setTitle("Processing...");
new TheTask().execute();
Run Code Online (Sandbox Code Playgroud)
将 AsyncTask 定义为活动类中的内部类
class extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try
{
JSONArray json = new JSONArray(executeHttpGet("http://m-devsnbp.insightlabs.cl/cuentos/json"));
talesList = jsonToHash(json);
}
catch (InterruptedException e)
{
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
adapter = new LazyAdapter(AcctivityName.this, talesList, 2);
list.setAdapter(adapter);
}
}
Run Code Online (Sandbox Code Playgroud)
2021 年 1 月更新
上面的答案已经过时了( asynctask 已被废弃)。还有其他更新 ui 的新方法。根据您是否使用协程或 rxjava,您可以轻松更新 ui。在这种情况下,最好提出一个新问题。
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |