0

我想添加使用arrayadapter填充我的listview,即时通讯使用simpleadapter。我想更改我的适配器的原因是,我可以使用方法notifyDataSetChanged(), 所以任何人都可以告诉我如何使用下面的代码做到这一点?我真的很感激它使用arrayadapter将项添加到列表视图

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); 
     } 
    }); 

} 
    } 
+0

当您的数据从服务器重新加载或当您在那时更改该数据。首先下载数据或更改数据。第二次调用 - adapter.notifiydatastatechange()。 – 2013-02-23 04:55:24

+0

你应该在列表上调用Adapter的'ListAdapter.this.notifyDataSetChanged()'函数。 – GrIsHu 2013-02-23 04:55:39

+0

@Grishu我使用该方法并在setListAdapter(adapter)下面贴上它;但仍然没有运气,日食不承认方法notifyDataSetChanged();在我的简单适配器中。 – jun 2013-02-23 05:11:58

回答

1

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

现在文档指出SimpleAdapter是“一个简单的适配器,用于将静态数据映射到视图”,这将更详细地讨论。 here。但事实是SimpleAdapter也适用于动态数据。

如果您仍然想切换到ArrayAdapter,那么您必须重写ArrayAdapter的getView()方法,因为ArrayAdapter每行只支持一个TextView(与支持将多个值映射到多个Views的SimpleAdapter相同行)。 有很多关于ArrayAdapter的教程,例如:这一个: http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example

+0

jeez这是在屁股这样的痛苦,我只是想使用notifyDataSetChanged()方法,我不能让它在我的代码工作,我的目标是只要我的饲料中有新的数据自动刷新我的列表视图。 – jun 2013-02-23 05:17:47

+0

您的应用如何以及在哪里通知了数据馈送的更改? – 2013-02-23 05:40:01

+0

即时通讯这样的应用程序就像一个rssfeed,这就是我想做的事情,当有新的数据被接收时,我想要做的事情是要通知/刷新我的列表视图,因为当我想刷新我的应用程序时,我通常关闭并打开我的应用程序。 – jun 2013-02-23 05:52:30