我试图在填充列表视图时显示 ProgressBar。
这是我的 XML
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:fadingEdge="none" >
</ListView>
<ProgressBar
android:id="@+id/doProgress"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center" />
</FrameLayout>
加载整个列表视图时,在 onPostExecuteMethod 中更改了 ProgressBar 的可见性。异步任务代码:
public class WhatToDoLoader extends AsyncTask<String, String, String> {
ProgressDialog progress = new ProgressDialog(WhatToDo.this);
String url = "http://wearedesigners.net/clients/clients12/tourism/fetchWhatToDoList.php";
final String TAG_MAIN = "item";
final String TAG_ID = "itemId";
final String TAG_NAME = "itemName";
final String TAG_DETAIL = "itemDetailText";
final String TAG_ITEM_IMAGE = "itemImages";
final String TAG_MAP = "itemMapData";
final String TAG_MAP_IMAGE = "mapImage";
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
adapter.notifyDataSetChanged();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
/*
* progress.setMessage("Loading What To Do List"); progress.show();
*/
}
@Override
protected String doInBackground(String... params) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(TAG_MAIN);
// TODO Auto-generated method stub
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(TAG_ID, parser.getValue(e, TAG_ID));
map.put(TAG_NAME, parser.getValue(e, TAG_NAME));
map.put(TAG_DETAIL, parser.getValue(e, TAG_DETAIL));
map.put(TAG_MAP, parser.getValue(e, TAG_MAP));
map.put(TAG_MAP_IMAGE, parser.getValue(e, TAG_MAP_IMAGE));
map.put(TAG_ITEM_IMAGE, parser.getValue(e, TAG_ITEM_IMAGE));
System.out.println("Test : " + parser.getValue(e, TAG_ID));
// adding HashList to ArrayList
whatToDoInfo.add(map);
publishProgress("");
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ProgressBar pb = (ProgressBar) findViewById(R.id.doProgress);
pb.setVisibility(pb.INVISIBLE);
}
}
当我运行代码时,它会引发以下异常。
* java.lang.IllegalStateException:适配器的内容发生了变化,但ListView没有收到通知。确保适配器的内容不是从后台线程修改的,而只是从 UI 线程修改的。*
但是当省略进度条功能时,相同的代码可以正常工作。我找不到哪里出错了。有人可以帮帮我吗 ?
先感谢您。