【发布时间】:2013-05-09 16:25:09
【问题描述】:
我正在从服务器获取 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();
}
如何从不同的线程更新我的观点?或者这是一种更好的方法?
【问题讨论】:
标签: android multithreading android-listview progressdialog