使用arrayadapter将项添加到listview

时间:2013-02-23 04:29:08

标签: android listview android-arrayadapter domparser notifydatasetchanged

我想使用arrayadapter添加填充我的listview,我目前正在使用simpleadapter。我想更改我的适配器的原因是我可以使用方法notifyDataSetChanged(), 所以有人能告诉我如何使用下面的代码吗?我真的很感激

 public class AndroidXMLParsingActivity extends ListActivity {

// All static variables
static final String URL = "https://news.instaforex.com/news";


// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_PUBDATE = "pubDate";
static final String KEY_DESCRIPTION = "description";



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

    final ArrayList<HashMap<String, Spanned>> menuItems = new ArrayList<HashMap<String, Spanned>>();

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

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, Spanned> map = new HashMap<String, Spanned>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_TITLE, Html.fromHtml(parser.getValue(e, KEY_TITLE)));
        map.put(KEY_PUBDATE, Html.fromHtml(parser.getValue(e, KEY_PUBDATE)));
        map.put(KEY_DESCRIPTION, Html.fromHtml(parser.getValue(e, KEY_DESCRIPTION)));

        // adding HashList to ArrayList
        menuItems.add(map);

    }

    // Adding menuItems to ListView

    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_TITLE, KEY_PUBDATE }, new int[] {
                    R.id.name, R.id.cost }); 

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String title = menuItems.get(position).get(KEY_TITLE).toString();
            String pubDate = menuItems.get(position).get(KEY_PUBDATE).toString();
        String description= menuItems.get(position).get(KEY_DESCRIPTION).toString();

    System.out.println("PubDate==>"+pubDate+"\n Description===>"+description);

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_TITLE, title);
            in.putExtra(KEY_PUBDATE, pubDate);
            in.putExtra(KEY_DESCRIPTION, description);
            startActivity(in);
        }
    });

}
  }

1 个答案:

答案 0 :(得分:1)

notifyDataSetChanged()是一个BaseAdapter方法,是SimpleAdapter和ArrayAdapter的父类。 SimpleAdapter和ArrayAdapter的主要区别在于提供给AdapterView的数据。虽然SimpleAdapter由XML数据支持,但ArrayAdapter由任意数据数组支持。 换句话说,不需要改变适配器的类型。

现在,文档指出SimpleAdapter是“将静态数据映射到视图的简单适配器”,这将更详细地讨论,例如: here。但实际上,SimpleAdapter也可以处理动态数据。

如果您仍想切换到ArrayAdapter,则必须覆盖ArrayAdapter的getView()方法,因为ArrayAdapter每行仅支持一个TextView(与支持将多个值映射到单个行的多个视图的SimpleAdapter不同) )。 有很多有关ArrayAdapter的教程,例如:这个: http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example