How to make a Binding data in a Listview in Xamarin.Android?

Asked

Viewed 177 times

0

I’m trying to do fills a listview with the data from a table, but so far I didn’t understand how to make the Binding date 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);
        }
    }
}

1 answer

1


To display only the names in Listview coming from the bank, simply insert the following line into your code.

lvUsers.Adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1, items);
  • The last parameter is an int items ???

  • @Marconciliosouza, the last parameter is a collection of strings.

  • Actually there is one more parameter that is missing, I just didn’t understand if it has to be the int of Listview.

  • @Marconciliosouza, do the following new Arrayadapter<string>(this,Resource.Layout.Simplelistitem1, items); and see if it works.

  • So from the error, you need to pass a bad parameter before the string collection.

  • @Marconciliosouza, here’s an example I put on github see if it helps https://github.com/juniorporfirio/ListViewAndroidXamarin

Show 1 more comment

Browser other questions tagged

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