locked
How do I get all items from a ListView? RRS feed

  • Question

  • Hello,

    I was wondering if there is no method to get all items in a ListView or am I just blind?

    Currently I use the following code to get all items in my ListView:

    //FavouriteStores is initialized with WinJS.Binding.List()

    //FavouriteListView is the ListView-control bound with the FavouriteStores.itemList var itemsInFavList = FavouriteListView.itemDataSource.getCount()._value; var favItems = []; for (var i = 0; i < itemsInFavList; i++) { favItems.push(FavouriteStores.itemList.getAt(i)); }


    but this way seems too complicated to me, especially because the IListDataSource - interface has methods for inserting, moving and removing and getting an item by index but no function for getting all items (http://msdn.microsoft.com/en-us/library/windows/apps/br211786.aspx).

    Is there a better way to get all items in the ListView?

    Friday, July 6, 2012 9:01 AM

Answers

  • Thats, what I've done in my code shown above, but I thought, it would go easier. Seems, like it does not.
    • Marked as answer by Dino He Friday, July 13, 2012 8:44 AM
    Thursday, July 12, 2012 2:15 PM

All replies

  • Your dataSource has all your items in your list view.  Just use that!

    Jeff Sanders (MSFT)

    Friday, July 6, 2012 12:13 PM
    Moderator
  • Thanks for your reply. But this did not worked for me. I've not found a method to get all items stored in the dataSource. I want to get all items and serialize them for storing them as JSON-String into the application folder for saving a user selection.

    Is there a good way for that?

    Thursday, July 12, 2012 7:22 AM
  • Afaik, most datasources will add a property 'totalCount' to the result when asking for their first item. So you ask the IListDataSource for the first item using itemsFromIndex, and then either check for the totalCount property and fetch the remaining items all at once or - in case the dataSource doesn't support totalCount - in batches of N items and stop when you find the 'atEnd' property to be true. One of those should be present at least.
    Thursday, July 12, 2012 8:32 AM
  • Thats, what I've done in my code shown above, but I thought, it would go easier. Seems, like it does not.
    • Marked as answer by Dino He Friday, July 13, 2012 8:44 AM
    Thursday, July 12, 2012 2:15 PM