0

I want to add populate my listview using arrayadapter, im currently using simpleadapter. the reason i want to change my adapter is so that i can use the method notifyDataSetChanged(), so can anyone show me how to do that using my codes below? i would really appreciate it

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

}
  }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
jun
  • 35
  • 2
  • 12
  • when your data is reload from server or when you change that data at that time. first download data or change data. 2nd call -- adapter.notifiydatastatechange(). – Dhaval Parmar Feb 23 '13 at 04:55
  • You should call Adapter's `ListAdapter.this.notifyDataSetChanged()` function on list. – GrIsHu Feb 23 '13 at 04:55
  • @Grishu i use that method and paster it below setListAdapter(adapter); but still no luck, eclipse doesnt recognize the method notifyDataSetChanged(); in my simpleadapter. – jun Feb 23 '13 at 05:11

1 Answers1

1

notifyDataSetChanged() is a BaseAdapter method, the parent class of SimpleAdapter and ArrayAdapter. SimpleAdapter and ArrayAdapter are different mainly with respect to the data provided to the AdapterView. While a SimpleAdapter is backed by XML data, an ArrayAdapter is backed by an array of arbitrary data. In other words there's no need to change the type of adapter.

Now the documentation states that a SimpleAdapter is "an easy adapter to map static data to views" which is discussed in more detail e.g. here. But fact is that a SimpleAdapter works with dynamic data too.

If you still want to switch to an ArrayAdapter then you have to overwrite the ArrayAdapter's getView() method because an ArrayAdapter supports only a single TextView per row (unlike the SimpleAdapter that supports mapping of multiple values to multiple Views for a single row). There are numerous tutorials on ArrayAdapter out there, e.g. this one: http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example.

Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • jeez this is such a pain in the ass,, i just want to use notifyDataSetChanged() method i can't get it work in my codes, my goal is just to automatic refresh my listview whenever there is a new data in my feed. – jun Feb 23 '13 at 05:17
  • How and where is your app notified about changes to the data feed? – Emanuel Moecklin Feb 23 '13 at 05:40
  • im using this app like an rssfeed, that is what i want to do to be notified/refresh my listview when there is new data to be received, because when i want to refresh my app i usually close and open my application.. – jun Feb 23 '13 at 05:52
  • A very simple solution would be to just create a new Adapter and call setAdapter on the ListView. My guess is that reading the feed is the costly operation while creating a new adapter is relatively cheap (depending on the frequency of the updates of course). – Emanuel Moecklin Feb 23 '13 at 06:05
  • can you show me how to do that in my code, please?? i admit im noob here, T_T – jun Feb 23 '13 at 06:14
  • You already have the code to create the Adapter: ListAdapter adapter = new SimpleAdapter(...); setListAdapter(adapter); – Emanuel Moecklin Feb 23 '13 at 06:37
  • then what should i need to input to make my program refresh using notifyDataSetChanged() ? – jun Feb 23 '13 at 06:58