我使用以下代码打开一个 XML 文件并在 ListView 中查看其信息。
我在代码的第一部分包含了一个 ProgressDialog,以便在文件加载之前显示它,一旦加载,ProgressDialog 就会消失。
问题是当文件已经加载时 ProgressDialog 正在显示。一直黑屏,直到加载文件,加载完成后显示ListView,同时显示ProgressDialog。
请帮助我。
谢谢
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog _progressDialog = new ProgressDialog(this);
_progressDialog.setIcon(R.drawable.icon);
_progressDialog.setTitle("Loading ...");
_progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
runOnUiThread(new Runnable() {
@Override
public void run() {
_progressDialog.show();
Thread t = new Thread() {
public void run(){
GetData();
_progressDialog.dismiss();
}
};
t.run();
}
});
}
public boolean GetData() {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
setContentView(R.layout.listplaceholder);
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(Main.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", "Naam:" + XMLfunctions.getValue(e, "name"));
map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "Score", "name" },
new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2 });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("name") + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
return true;
}
}
最佳答案
使用AsyncTask并在 onPreExecute()
中显示进度对话框:
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog _progressDialog = new ProgressDialog(this);
_progressDialog.setIcon(R.drawable.icon);
_progressDialog.setTitle("Loading ...");
_progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
new AsyncTask<Void, Void, Void>() {
protected Long doInBackground(Void ... urls) {
GetData();
}
protected void onPostExecute(Void result) {
_progressDialog.dismiss();
}
protected void onPreExecute(Void no) {
_progressDialog.show();
}
}.execute();
}
关于android - 在加载 XML 文件之前显示 progressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7853881/