0

我的代码是这个http://jsfiddle.net/navdeep/7TLDu/

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{   

    ArrayList<HashMap<String,String>> items = null;
    int eventType = parser.getEventType();
    CustomObject currentProduct = null;
    HashMap<String, String> item = null;
    while (eventType != XmlPullParser.END_DOCUMENT){
        String name = null;
        switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                 item = new HashMap<String,String>();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equals("member")){
                    currentProduct = new CustomObject();
                } else if (currentProduct != null){
                    if (name.equals("name")){
                        currentProduct.name = parser.nextText();
                        item.put( "line1", currentProduct.name);
                    } else if (name.equals("position")){
                        currentProduct.position = parser.nextText();
                        item.put( "line2", currentProduct.position);
                    } 
                }


                Log.i(TAG, "Node parsing");
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("member") && currentProduct != null){
                    items.add(item);
                }
        }
        eventType = parser.next();
    }
    Log.i(TAG, "Parsing complete");

    sa = new SimpleAdapter(this, items,
      android.R.layout.two_line_list_item ,
      new String[] { "line1","line2" },
      new int[] {android.R.id.text1, android.R.id.text2});
    setListAdapter( sa );


  //  printProducts(items);
    Log.i(TAG, "setListAdapter");
}

解析 XML 后,我需要在我的 layout.xml 中的 ListView 中显示数据。我向您展示的代码不会在 ListView 中显示列表。

任何人请帮忙。

谢谢!!

4

1 回答 1

0

我已经浏览了您的代码...尝试在 onCreate 中分配分配适配器...希望这不会在函数中工作...更多参考这里是简单列表适配器的虚拟代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class MyTwoListItemsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };

SimpleAdapter adapter = new SimpleAdapter(this, list,
    android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}

private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Android", "Mobile"));
list.add(putData("Windows7", "Windows7"));
list.add(putData("iPhone", "iPhone"));
return list;
}

private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}

}

更多描述通过以下链接...... http://www.vogella.com/articles/AndroidListView/article.html

希望它会帮助你...

于 2013-06-30T09:42:08.053 回答