How to view in a data listview via web api?

Asked

Viewed 113 times

-1

I am having difficulties to accomplish this task, need to fetch data that are stored via mysql on a website and display them on my app being developed on Xamarin Forms, for this I need to make a 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 Clienterequest

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

and in my codeBehind is so provisionally

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

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

http://ativoproject.ebasesistemas.com.br

And here would be the url of the given function:

data/_get clientescolder.php

How would it look for me to pass these urls and recover those data by displaying them in listview?

1 answer

1


Hello, Just a tip, it would be much better to use the standard MVVM (Model-View-Viewmodel).

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 the codeBehind I just pointed to viewModel

public PersonsView()
    {
        InitializeComponent();

        BindingContext = new PersonsViewModel();
    }

and so became my Viewmodel

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’ve helped :)

I put the code on the github

  • exactly, I did it and everything was ok

Browser other questions tagged

You are not signed in. Login or sign up in order to post.