0
votes

I'm having trouble copying data from one ObservableCollection to another. I have an api call GetItemsAsync from http that puts the response into a model called ShipList.cs. Inside of ShipList.cs there is ShipCatalog[] ships. I have created a second model called HangarList.cs with HangarCatalog[] hangars. I have a page that displays the master list of ships (ShipsList) I want the user to select the ship (ShipList.name is bound to this particular ListVIew. I tried to use .Where() to filter ShipList to only the match to the selected item and copy that data to HangarCatalog. I'm getting Cannot convert GallogForms.Api.ShipCatalog to GallogForms.Api.HangarCatalog using the following code. ViewModel


      private ShipCatalog _selectedShip;
       public ShipCatalog SelectedShip
        {
        get {return _selectedShip; }
    set
    {if (_selectedShip != value)
    _selectedShip = value;
    id = _selectedShip.id;
    CopyShipData();


    private async void CopyShipData()
            {
                var _container = Items.Where(s => 
        s.name.FirstOrDefault().ToString() == id.ToString()).ToList();
            foreach (var item in _container.Where(s => 
    s.name.FirstOrDefault().ToString() == id.ToString()).ToList())

              //  var items = await _gallogClient.GetItemsAsync<ShipList>();
             //   foreach (var item in items.ships.Where(s => 
      //  s.name.FirstOrDefault().ToString() == id.ToString()).ToList())
                {
                    Hangars.Clear();
                    Hangars.Add(item);
                }
            }

I haven't found any answer yet, and I've read plenty, that can address my situation. myShipsList is bound to a new model I've created in the API that perfectly mirrors ShipCatalog[].

I've also keep running across answers that suggest ListViewItem.Item or in my case SuggestedShipView.Items. .Items is not an option for my ListViews in the view model.

AddShipPage.xaml


    <StackLayout Orientation="Vertical">
            <SearchBar x:Name="HangarListView" Text="Add To Your Fleet!"
                       TextChanged="HangarList_TextChanged" 
                       BackgroundColor="Azure"
                       />
            <Grid>
                <ListView x:Name="SuggestedShipView" 
                          ItemsSource="{Binding Items}"
                          SelectedItem="{Binding selectedShip}"
                          BackgroundColor="Silver">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>

                  .....................

ShipList.cs (API Query)


    [ApiPath("ships")]
        public class ShipList : ApiQueryable
        {
            public ShipCatalog[] ships { get; set; }
        }
        public class ShipCatalog : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged; 
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

            public int id { get; set; }
            public string name { get; set; }
            public string uri { get; set; }
            public int rsi_id { get; set; }
            public string img { get; set; }
            public string mfr { get; set; }
            public string flyable { get; set; }
            public string scu { get; set; }
            public string value { get; set; }
            public string bgcolor { get; set; }
            public string color { get; set; }
            public string role { get; set; }
            public bool _isVisible { get; set; }
            public bool IsVisible
            {
                get { return _isVisible; }
                set
                {
                    if (_isVisible != value)
                    {
                        _isVisible = value;
                        OnPropertyChanged();
                    }
                }
            }
        }

}

HangarList mirrors ShipList perfectly with the exception it's named HangarList, public HangarCatalog[] hangars

And Finally, the query which populates ShipCatalog[] AddShipViewModel



      Items.Clear();
            var items = await _gallogClient.GetItemsAsync<ShipList>();
            foreach (var item in items.ships.ToList())
            {
                Items.Add(item);


      }

No error messages per se, but I have not been able to structure a method to complete this task. If you would like to see the entire project to see more of what I have going on, http://github.com/dreamsforgotten/GallogMobile

1
when the user selects an item from the first list, what data do you want displayed in the second list? - Jason
Name, manufacturer, value, scu, value, flyable, and image in a grid layout I haven't designed yet. Edit : I might need to tap this list for its values in other parts of the app in the future if why I created the hangarlist model. - Dreamsforgotten
First, that sort of UI really doesn't work in a ListView. Second, it sounds like you want the datasource for the 2nd control to just be the item selected in the first. You shouldn't need to do any complex transformations or copying of the data. - Jason
That's exactly what I want to do, and the second list view may or may not remain a list view. Currently i was planning on replicating shippage. Xaml the same data displayed. I'm not so much concerned about the list view itself so much as populating hangar catalog with the selected items from SuggestedShipList. - Dreamsforgotten
"populating hangar catalog with the selected items from SuggestedShipList" - this implies that you can selected multiple items from the first list? - Jason

1 Answers

0
votes

when you tap or select an item in a ListView, the second parameter of the event handler will contain a reference to the item selected/tapped. You just need to cast it to the correct type. Then you can reference all of its properties as needed

private void SuggestedShipView_ItemTapped(object sender, ItemTappedEventArgs e)
{
  // e.Item is the specific item selected
  ShipCatalog ship = (ShipCatalog)e.Item;

  // you can then use this ship object as the data source for your Hangar list/control,
  // and/or add it to another List that is just the items the user has selected

}