0

我正在尝试在加载作为源 xml 的列表视图之前实现一个显示进度对话框的线程,但是在线程中这样做是一个错误警告,该函数 Lazy Adapter 也需要有一个线程,想要一些建议这个问题,谢谢。

主要的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    handler = new Handler();
    new Thread(){            
         public void run() {
             runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");                      
                }
            });             
             ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

                XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(URL); // getting XML from URL
                Document doc = parser.getDomElement(xml); // getting DOM element

                NodeList nl = doc.getElementsByTagName(KEY_SONG);


                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(KEY_ID, parser.getValue(e, KEY_ID));
                    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                    map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                    map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                    // adding HashList to ArrayList
                    songsList.add(map);
                }

                lista=(ListView)findViewById(R.id.list);

                adapter=new LazyAdapter(this, songsList); // ERROR APPEARS HERE 
                lista.setAdapter(adapter);


            progressDialog.cancel();
        }
     }.start();
}

LazyAdapter.java

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}
4

2 回答 2

0

UI界面只在主线程中

progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");  in child thread

所以有这个问题

于 2012-04-25T02:13:40.457 回答
0

我已尝试修复您的代码,您可以试试这个:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

handler = new Handler();
progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
new Thread(){            
     public void run() {

         ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML from URL
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);


            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(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    lista=(ListView)findViewById(R.id.list);

                    adapter=new LazyAdapter(this, songsList); // ERROR APPEARS HERE 
                    lista.setAdapter(adapter);


                    progressDialog.cancel();
                }
            });             

        }
     }.start();
}
于 2012-04-25T04:24:33.503 回答