Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So i haven't been able to find anything specific to Windows 8/8.1 as i am doing this project in the Windows store apps template.

I have got 2 list views of which i am removing and adding values between the two, the issue i am currently having is when attempting to retrieve the items and their values from the list that i am adding items to.

I am able to use a for loop to find out how MANY items are in the item collection of the listview, however that seems to be all i can do, I can't seem to use a foreach loop (which makes this that much more annoying) as it causes the app to simply crash when it comes to searching for the items in the listview.

So my question is (finally): How do i access the items that have been added to the ListView.Items, ItemCollection and retrieve said items properties and their values (i.e the name, the address, the phone number.. whatever this may be)

I've spent quite a bit of time on this and it's frustrating, i've though of using lambda expressions, but not 100% sure on wether this would actually work in this case.

Thanks for any help you can provide.
Posted
Comments
Herman<T>.Instance 16-Jun-15 10:54am    
There is no ListView.Items.Count property?
Herman<T>.Instance 16-Jun-15 10:55am    
On what code you are stuck. We cannot see your code, so it is a little hard to spot the problem.
Maciej Los 16-Jun-15 15:19pm    
What exactly have you tried till now?
Member 11650340 16-Jun-15 17:13pm    
@digimanus There was no code i was stuck on as none of the attempts i had actually worked I was able to iterate through the ItemCollection of the ListView via a FOR loop, but i wasn't able to do anything besides that.

@Maciej I was able to iterate through the ItemCollection as i said to digimanus, But wasn't able to do anything else, i attempted to do a Foreach loop for ItemCollection, ListViewItem and FavouritesItems (the custom object the list is holding), i was later informed by an instructor that for Windows Store Apps.. this wouldn't work.

@digimanus, Maciej.. I posted a solution to this as i figured it out in the early morning today, hopefully with my explanations along with the code i've made it a bit more clear on what i was trying to do, i apologize for not being more detailed in my original question.

1 solution

To the people that commented, thank you, sorry for things being a little.. less detailed when it comes to code portions, I wasn't able to show this code as i don't have anything attempting the retrieve items as i kept failing at doing so.. HOWEVER i figured this out this morning after i dropped my partner off to work.

This is my code solution:
C#
private async void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sortedItems = new List<favouritesitems>();
                for (int i = 0; i < lvFavourites.Items.Count(); i++)
                {
                    lvFavourites.SelectedIndex = i;
                    FavouritesItems item = (FavouritesItems)lvFavourites.SelectedItem;
                    sortedItems.Add(new FavouritesItems(item));
                }
                await SaveObjectToXml<list><favouritesitems>>(sortedItems,  "favouritelistItems.xml");
                MessageDialog msg = new MessageDialog("Favourites have been successfully saved to file");
                await msg.ShowAsync();
            }
            catch (Exception ex)
            {
                MessageDialog msg = new MessageDialog("Unknown Error has occured: " + ex.ToString());
                await msg.ShowAsync();
            }
        }</favouritesitems></list></favouritesitems>


What i had to do was add another constructor to my FavouritesItems Class (which is the object that is being added to the ListView.Items Item Collection)

This is my FavouritesItems Class:

C#
public class FavouritesItems
    {
        private FavouritesItems selectedItem;

        public string Section { get; set; }
        public string Destination { get; set; }

        public FavouritesItems(string section, string destination)
        {
            this.Section = section;
            this.Destination = destination;
        }

        public FavouritesItems()
        {
        }

        public FavouritesItems(FavouritesItems selectedItem)
        {
            this.Section = selectedItem.Section;
            this.Destination = selectedItem.Destination;
        }
    }


Using this new constructor i was able to grab a COMPLETED item (both the section and destination are added seperately as strings, i.e ListView.Items.Add(new FavouritesItems("Sight-Seeing", "Melbourne"); etc) and was able to retrieve the attribute values specifically and seperately.

Considering all this was in a FOR loop, i was able to do this for each item in the list.. one at a time. By ensuring that the selectedIndex was equal to the FOR loop integer i.

Hopefully this helps anyone else that might have a similiar issue to mine when it comes to Windows Store Apps and using ListView's and their ItemCollections.

Note: i know the first code segment is kind of.. well oddly formatted.. not sure how to fix it. (posted code from visual studio into the tags and it ended up like it looks)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900