0

I have code which takes in data from Flickrs Rest service and populates a ListView. This code works fine and when I run my app I can search for photos and be displayed a list of them. However I want to then get a single photos data but when I try to access this data from the ListView it's completely empty (Iv debugged it and it just contains null entries). I don't have a lot of experience with C# so could anyway advise me as to why I would be getting null results?

        private async void ParseFlickrResponse(HttpResponseMessage response)
    {

        XDocument xml = XDocument.Parse(await response.Content.ReadAsStringAsync());          
        var photos = from results in xml.Descendants("photo")
                     select new FlickrImage
                     {
                         ImageId = results.Attribute("id").Value.ToString(),
                         FarmId = results.Attribute("farm").Value.ToString(),
                         ServerId = results.Attribute("server").Value.ToString(),
                         Secret = results.Attribute("secret").Value.ToString(),
                         Title = results.Attribute("title").Value.ToString()
                     };

        FlickrListView.ItemsSource = photos;




    }

EDITED

Current code:

enter code here:

private async void ParseFlickrResponse(HttpResponseMessage response) {

        XDocument xml = XDocument.Parse(await response.Content.ReadAsStringAsync());          
        var photos = from results in xml.Descendants("photo").ToList()
                     select new FlickrImage
                     {
                         ImageId = results.Attribute("id").Value.ToString(),
                         FarmId = results.Attribute("farm").Value.ToString(),
                         ServerId = results.Attribute("server").Value.ToString(),
                         Secret = results.Attribute("secret").Value.ToString(),
                         Title = results.Attribute("title").Value.ToString()
                     };


        FlickrListView.ItemsSource = new ObservableCollection<FlickrImage>(photos);


    }

    private void GetPhotoSource(object sender, ItemClickEventArgs e)
    {

        int inx = FlickrListView.SelectedIndex;
       // FlickrImage t = lst.First();
        FlickrImage t = lst.ElementAt(inx);
        MyImage.Source = new BitmapImage(new Uri(t.ImageUrl.ToString(), UriKind.Absolute));  


    }
Stephen Hynes
  • 22,071
  • 6
  • 18
  • 20

1 Answers1

0

Try adding a .ToList() at the end of your LINQ.

*EDIT (comments summary)

You are handling ItemClick event which seems to be raised before the selection properties on the ListViewBase change. Since these are not updated at this point - you can check e.ClickedItem and cast it to FlickrImage to get your clicked item.

If you do want to work with selection properties - you should be handling the SelectionChanged event.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • So it would look like 'var photos = from results in xml.Descendants("photo").ToList()' ? – Stephen Hynes Oct 23 '12 at 15:51
  • yes, or if you are planning on making the list editable - do FlickrListView.ItemsSource = new ObservableCollection(photos); – Filip Skakun Oct 23 '12 at 15:53
  • The reason why is I think there might be some problems in how ListView handles an ItemsSource that is a LINQ query or perhaps it can't find the FlickrImage instances first added because a requery generates new ones. – Filip Skakun Oct 23 '12 at 15:55
  • So should it be possible to then do a for each loop on the ListView ala ' foreach (FlickrImage item in FlickrListView.SelectedItems)' . It comes back to having this problem when i use this method SelectedItems it returns nothing – Stephen Hynes Oct 23 '12 at 16:07
  • This solution would be perfect if I could get SelectedIndex to return something that wasn't -1. – Stephen Hynes Oct 23 '12 at 16:15
  • Does it still happen after you materialize your LINQ query by calling ToList() or make it into an ObservableCollection? – Filip Skakun Oct 23 '12 at 16:17
  • Yes it does, updated to the OP to show the current state. Inx gets set to -1 and causes an exception. – Stephen Hynes Oct 23 '12 at 16:22
  • Perhaps your FlickrImage needs Equals()/GetHashCode() overrides... See http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c – Filip Skakun Oct 23 '12 at 16:23
  • I just noticed you are probably doing the check in ItemClick handler of your ListView and it is possible SelectedIndex isn't set by that time, but you should be able to get e.ClickedItem instead. – Filip Skakun Oct 23 '12 at 16:27
  • So what can I do with e.ClickedItem it only implements some standard methods? – Stephen Hynes Oct 23 '12 at 16:30
  • The ListView seems to stay empty regardless oddly – Stephen Hynes Oct 23 '12 at 16:54
  • You need to also set IsItemClickEnabled="True". Your e.ClickedItem should be your FlickrImage, you just need to cast it. – Filip Skakun Oct 23 '12 at 17:06