Fill a listview with an API

0

I have been trying to fill out a ListView with a MYSQL query but I do not have any favorable results, I want to implement the use of adapter and assign in the listview only the Name and Surname for each element but when I try to assign my data collection it appears the following error:

"Unable to convert from 'System.Collections.ArrayList' to 'int'"

My code is as follows:

async void CargarMaestros()
        {
             List<Maestro> _maestros;
             ListView _lvwMaestros;
             _maestros = await ClienteMaestros.ObtenerTodas();
             ArrayList items = new ArrayList();
                for (int i = 0; i < _maestros.Count; i++)
                   {
                      items.Add(_maestros[i].Nombre + "" + _maestros[i].Apellidos);
                   }
              var adaptador = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);

              _lvwMaestros.Adapter = adaptador;
      }

SOLUTION

    using System.Linq; //HACES USO DE LINQ

    async void CargarMaestros()
    {

       _maestros = await ClienteMaestros.ObtenerTodas();

      var itemsLista = _maestros.Select(m => $"{m.Nombre} \t {m.Apellidos}") .ToArray();

      var adaptador = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, itemsLista);

      _lvwMaestros.Adapter = adaptador;
   }

GREETINGS:)

    
asked by Antonio Labra 05.05.2018 в 07:37
source

1 answer

0

The problem is, in that you are declaring the ArrayList wrongly, you need to specify the type of creation in its generic parameter.

You do this:

ArrayList items = new ArrayList();

Instead of doing this:

List<string> items = new List<string>();

Another thing is that you are only changing the reference of a list, not changing its values.

You do this:

_maestros = await ClienteMaestros.ObtenerTodas();

Instead of doing this:

_maestros.AddRange(await ClienteMaestros.ObtenerTodas());

As a result you would have 2 references pointing to the same collection. However with AddRange you are creating a copy of a collection (adding data from one list to another list). In your case you will not notice the difference because they are local variables. But you must have that pending to avoid future problems with lists.

    
answered by 05.05.2018 в 12:51