Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add items to ListView on Android in Xamarin application

I'm trying to remix the base Android advice for adding items to a ListView in a Xamarin application, but so far I'm failing.

In Xamarin Studio, I've created an Android App targeting Latest and Greatest, and all default settings. I then added a ListView to my activity and gave it an id of @android:id/list. I've changed the activity's code to this:

[Activity (Label = "MyApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : ListActivity
{
    List<string> items;
    ArrayAdapter<string> adapter;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);
        items = new List<string>(new[] { "Some item" });
        adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleListItem1, items);
        ListAdapter = adapter;

        FindViewById<Button> (Resource.Id.myButton).Click += HandleClick;
    }

    protected void HandleClick(object sender, EventArgs e) 
    {
        items.Add ("Another Item!");
        adapter.NotifyDataSetChanged ();
        Android.Widget.Toast.MakeText (this, "Method was called", ToastLength.Short).Show();
    }
}

I build the app and run it on my Nexus 5 device. The application starts fine, I can click the button, and see the debugger hit the handler. The debugger shows no other problems, both items.Add and the NotifyDataSetChanged methods are called without error, and the Toast shows up on my device's screen.

However, the item "Another Item!" does not appear in my list.

I do note that there's one big difference between the linked question and my solution. Where the linked question has code like this:

setListAdapter(adapter);

I instead did:

ListAdapter = adapter;

Because the setListAdapter method isn't available in my Xamarin solution, and I had assumed the property setter was meant to do the same.

Long story short: what do I need to do to dynamically add items to my ListView?

like image 202
Jeroen Avatar asked Sep 14 '15 14:09

Jeroen


People also ask

How to use ListView in Xamarin Android?

This file defines the layout for each item that will be placed in the ListView . Notice that this does not load a layout file for the Activity (which you usually do with SetContentView(int) )). Instead, setting the ListAdapter property automatically adds a ListView to fill the entire screen of the ListActivity .

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.


1 Answers

You are adding item in your list but the adapter isn't aware of that list. What you should do is add the item to the adapter:

adapter.Add ("Another Item!");
like image 158
Giorgi Avatar answered Sep 28 '22 06:09

Giorgi