3

我正在开发我的第一个 Android 应用程序,我想显示一个进度对话框,直到在 doInBackground 方法中处理一个 xml 文件。加载此活动以响应 onclick 事件。出乎意料的是,活动需要几秒钟才能显示出来,并且进度对话框显示的时间很短(几毫秒)。

这是我的代码。我使用 AsyncTask 作为内部类。我只是找不到哪里出错了。

public class WhatToSee extends ListActivity {
ListView whatToSee;
ArrayList<HashMap<String, String>> whatToSeeInfo = new ArrayList<HashMap<String, String>>();
WhatToSeeAdapter adapter;
String cityName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whattosee);
    cityName = getIntent().getStringExtra("name");
    whatToSee = (ListView) findViewById(android.R.id.list);
    adapter= new WhatToSeeAdapter(this, whatToSeeInfo);
    whatToSee.setAdapter(adapter);
    new WhatToSeeLoader().execute();
}




public class WhatToSeeLoader extends AsyncTask<Void, String, String> {
    ProgressDialog progress = new ProgressDialog(WhatToSee.this);
    String url = "http://wearedesigners.net/clients/clients12/tourism/fetchWhatToSeeList.php";
    final String TAG_MAIN = "item";
    final String TAG_ID = "itemId";
    final String TAG_NAME = "itemName";
    final String TAG_DETAIL = "itemDetailText";
    final String TAG_MAP = "itemMapData";
    final String TAG_ITEM_IMAGE = "itemImages";
    final String TAG_MAP_IMAGE = "mapImage";
    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);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        progress.setMessage("Loading What To See List");
        progress.setIndeterminate(true);
        progress.show();

    }

    @Override
    protected String doInBackground(Void... params) {
        // 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));

            // adding HashList to ArrayList
            whatToSeeInfo.add(map);
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        adapter.notifyDataSetChanged();
        progress.dismiss();

    }

}

}

有人可以帮我吗?先感谢您。

4

1 回答 1

2

很可能调用String xml = parser.getXmlFromUrl(url); // getting XML会减慢您的代码。它在主线程中执行,我希望它(从它的名字)做一些与网络相关的工作,这些工作基本上不应该在 UI 线程中完成(例如在 YoursdoInBackground方法中)。
因此,要修复尝试将与 xml 相关的初始化人员移到doInBackground().

于 2012-11-01T15:02:39.480 回答