How to do a data binding in a ListView in Xamarin.Android?

0

I'm trying to make a listview fill with the data in a table, but so far I do not understand how to do the data binding on it.

XML;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    tools:menu="top_menus">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/LayoutConteudo">
        <ListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lvUsers" />
    </LinearLayout>
</RelativeLayout>

C #;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using StockControl.Classes;
using static Android.Resource;

namespace StockControl
{
    [Activity(Label = "Stock Control", MainLauncher = true, Icon = "@drawable/Logo", Theme = "@style/MyTheme")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
            SetActionBar(toolbar);
            ActionBar.Title = "Rohr";

            var path = Resources.GetString(Resource.String.database);
            CreateDataBase db = new CreateDataBase(path);
            db.CreateDatabaseAndTables();

            Usuarios user = new Usuarios();
            var usuarios = user.getUsuario(1, path);

            var lvUsers = FindViewById<ListView>(Resource.Id.lvUsers);

            string[] items;
            foreach (var item in usuarios)
            {
                items = new string[] { item.nome };
                var data = item.dataNascimento;
            }

            // data binding aqui 
            // lvUsers

        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.top_menus, menu);
            return base.OnCreateOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            Toast.MakeText(this, "Action selected: " + item.TitleFormatted,
                ToastLength.Short).Show();
            return base.OnOptionsItemSelected(item);
        }
    }
}
    
asked by anonymous 22.03.2017 / 21:10

1 answer

1

To display only the names in the ListView from the database, simply insert the following line in your code.

lvUsers.Adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1, items);
    
23.03.2017 / 04:58