ListActivity未显示列表

时间:2014-08-12 21:13:15

标签: android android-listview progressdialog

我有一个LIstActivity,它使用URL在线加载一个列表,但是如果我使用进度环并且在线程中首先启动环,则只有在没有使用可运行对象或线程的情况下才能成功加载ListActivity或者runnable然后进度环显示并且它的工作结束但我只获得ListActivities标题但没有ListActivity的列表或正文。

以下是我的代码:

ProgressDialog ringProgressDialog;
private Handler handler = new Handler();

private static XMLParser parser;
private static String xml;
private static Document doc;

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

    ringProgressDialog = ProgressDialog.show(this, "Please wait ...",
            "Beer's Downloading!", true);
    ringProgressDialog.setCancelable(true);
    Thread t = new Thread() {
        public void run() {
            menuItems = new ArrayList<HashMap<String, String>>();
            Log.d("CHECKER", "just entered the Activity");
            parser = new XMLParser();
            xml = parser.getXmlFromUrl(URL); // getting XML
            doc = parser.getDomElement(xml); // getting DOM
            Log.d("CHECKER", "parsing the elements from XML");
            NodeList nl = doc.getElementsByTagName(KEY_RECORD);
            // looping through all item nodes <item>
            Log.d("CHECKER", "started inputting values into map one by one");

            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_CODE, parser.getValue(e, KEY_CODE));
                map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_COMPANY, parser.getValue(e, KEY_COMPANY));
                map.put(KEY_CATNUM, parser.getValue(e, KEY_CATNUM));
                map.put(KEY_CATNAME, parser.getValue(e, KEY_CATNAME));
                map.put(KEY_SUBLTR, parser.getValue(e, KEY_SUBLTR));
                map.put(KEY_SUBNAME, parser.getValue(e, KEY_SUBNAME));
                // adding HashList to ArrayList
                menuItems.add(map);

            }
            makeIt();

            handler.post(new Runnable() {
                public void run() {
                    ringProgressDialog.dismiss();
                };
            });
        }
    };
    t.start();

    // new ProgressTask(AndroidXMLParsingActivity.this).execute();
}

private void makeIt() {

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item, new String[] { KEY_NAME, KEY_SUBNAME,
                    KEY_CATNUM }, new int[] { R.id.name, R.id.desciption });

    // setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String description = ((TextView) view
                    .findViewById(R.id.desciption)).getText().toString();
            HashMap<String, String> extractItems = menuItems.get(position);
            String code = extractItems.get(KEY_CODE);
            Toast.makeText(getApplicationContext(), code,
                    Toast.LENGTH_SHORT).show();
            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_SUBNAME, description);
            in.putExtra(KEY_CODE, code);
            startActivity(in);
        }
    });
}

基本上我想显示一个对话框,同时提取xml元素并解析值并将其加载到Hashmap中,然后让dialoog消失。

1 个答案:

答案 0 :(得分:1)

您正在单独的线程中设置ListView,该线程不会通过从活动中调用来更新您需要在Ui线程中设置ListView的{​​{1}}。

<强>样品:

ListView