解析xml后,想要使用SimpleListAdapter显示数据

时间:2013-06-30 09:04:17

标签: android android-layout android-listview

我的代码是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中显示列表。

任何人都可以帮助。

谢谢!

1 个答案:

答案 0 :(得分: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

希望它能帮到你......