How to display in a listview data via web api?

-1

I'm having trouble performing this task, I need to fetch data that is stored via mysql on a website and display them in my app that is being developed in Xamarin Forms , so I need to webservice . I have the following context: In my XAML file

<Label Text="Cliente" Grid.Row="0" Grid.Column="2" FontAttributes="Bold" />
                    <ListView x:Name="coluna2" Grid.Column="2" Grid.Row="1"  SeparatorColor="White"
                SeparatorVisibility="Default"/>

I created a class called ClientRequest

public  class ClienteRequest
    {
        public string cpfcnpj { get; set; }
        public string nomefantasia { get; set; }
    }

and in my codeBehind is thus provisionally

 List<String> itens2 = new List<String>()
            {
                "dados do banco"
            };
        coluna2.ItemsSource = itens2;

Here would be the address where the data will be consumed:

  

link

And here would be the url of the given function:

  

data / _gettingcollector.php

How would I get past these urls and retrieve this data by displaying them in the listview?

    
asked by anonymous 20.02.2018 / 00:19

1 answer

1

Hello, Just a hint, it would be much better to use the MVVM (Model-View-ViewModel) pattern.

I created a View (xaml)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="SampleListBinding.Views.PersonsView">
 <ListView x:Name="MyListView"
        ItemsSource="{Binding Persons}"
        ItemTapped="Handle_ItemTapped"
        CachingStrategy="RecycleElement">
        <!--Custom View Cells-->
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding FirstName}" />
                    <Label Text="{Binding LastName}"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
   </ListView>

In codeBehind I just pointed to the viewModel

public PersonsView()
    {
        InitializeComponent();

        BindingContext = new PersonsViewModel();
    }

And so my ViewModel was

public class PersonsViewModel:ViewModelBase
{
    private ObservableCollection<Person> persons;

    public ObservableCollection<Person> Persons
    {
        get { return persons; }
        set
        {
            persons = value;
            OnPropertyChanged();
        }
    }

    public PersonsViewModel()
    {
        GetPerson();
    }

    public void GetPerson()
    {
        Persons = new ObservableCollection<Person>();
        for(byte i =0; i< 20; i++)
        {
            Person p = new Person();
            p.FirstName = $"Person FirstName {i}";
            p.FirstName = $"LastName {i}";
            Persons.Add(p);
        }
    }

}

I hope I have helped:)

put the code in github

    
21.02.2018 / 03:59