-1

After long efforts, I managed to pull data from the api; When I compile and click on ListVeiw1 screen, I can't get ip or name, What is the solution? Thanks.

<ListView SelectionMode="Single" ItemSelected="ListView1_ItemSelected" x:Name="ListView1".....

private async void Button_Clicked(object sender, EventArgs e)
        {
            List<TodoItem> itemsNew = new List<TodoItem>();
            using (var ic = new HttpClient())
            {
                using (var response = await ic.GetAsync("http://adress.com/api/items"))
                {
                    var content = await response.Content.ReadAsStringAsync();
                    itemsNew = JsonConvert.DeserializeObject<List<TodoItem>>(content);                 
                    ListView1.ItemsSource = itemsNew;
                }
            }
        }


  private void ListView1_ItemSelected(object sender, SelectedItemChangedEventArgs e)
  {
     string myname = e.SelectedItem.ToString();
  }
  • "I can't get ip or name" - what does this mean? Are you saying your ListView is not showing any data? Please be specific about the exact problem you are having. You have not shown your `ItemTemplate` or the definition of `TodoItem` so its very difficult to provide advice. – Jason Nov 11 '21 at 15:02

1 Answers1

-1

you need to cast the SelectedItem to the correct type before you can access its properties

private void ListView1_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
   var item = (TodoItem)e.SelectedItem;
   // now you can access any properties of item
}
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Jason, my dear friend This brought everything of my chosen one, I need to get his name or ID number from it, originally I was stuck there var item = (TodoItem)e.SelectedItem; DisplayAlert("Selected", item.ToString(), "OK"); – İsmail Çakmak Nov 11 '21 at 17:43
  • I have no idea what that means. Are you still having problems? Or is your problem solved? – Jason Nov 11 '21 at 20:23