The best option would be to update the list that the ArrayAdapter uses by working with the ArrayAdapter methods. Use the methods on the ArrayAdapter object. Add, Insert, Remove, Clear for example. This should dynamically update the list.
You can also use OnNotifyDataSetChanged, but the best practice would be to use the ArrayAdapter's methods to update the list.
If your list view name is : listView and shows a list of persons' names.
Every item is like : public class Person { public string name {get;set;} }
In your Button event : private void addPerson_click(object sender, EventArgs e) { Person person=new Person(){name="Mabrouk"}; // or get data from an other way... listView.ItemsSource.Add(person); }
In your XAML page, you should have something like : <ListView x:Name="listView" VerticalOptions="FillAndExpand" SeparatorVisibility="None"> <ListView.ItemTemplate> <DataTemplate> <Label Text="{Binding name}" /> </DataTemplate> </ListView.ItemTemplate> </ListView>
If you are working in Xamarin.Forms, @Mabrouk has the correct answer. If you are working in Xamarin.Android, you'll need to work with the array adapter object.
@JBeck , yes my answer was for Xamarin.Forms..
But in xamarin.android, @FilippoBiondi needs to something like this :
1. having a private List<Person> _persons=new List<Person>(); before,
2. Add the new person to _persons in the button click event like : private void addPerson_click(object sender, EventArgs e) { Person person=new Person(){name="Mabrouk"}; // or get data from an other way... _persons.Add(person); ArrayAdapter adapter = new ArrayAdapter (this, Resource.Layout.TextViewItem, _persons); listView.Adapter=adapter; }
3. Note : the code is not perfect it will be better when you customize your Adapter..
Your code will work, but it will result in rebuilding the list every time the user clicks the add button, which is very inefficient.
In the following code, the button click event adds the code to the Adapter's existing list. Note that I've added the ListViewItem.axml to the Resources/layout folder, with a TextView with ID label. I've attached a screenshot of it in action.
MainActivity
using System;
using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
namespace AndroidSandbox {
[Activity(Label = "AndroidSandbox", MainLauncher = true, Icon = "@drawable/icon")]
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);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += addPerson_click;
var listView = FindViewById<ListView>( Resource.Id.PeopleListView );
listView.Adapter = new ArrayAdapter<string>(this, Resource.Layout.ListViewItem, Resource.Id.label);
}
private void addPerson_click(object sender, EventArgs e) {
ArrayAdapter<string> adapter = (ArrayAdapter<string>)FindViewById<ListView>(Resource.Id.PeopleListView).Adapter;
adapter.Add("New Person Name");
}
}
}
Answers
Hi @FilippoBiondi
The best option would be to update the list that the ArrayAdapter uses by working with the ArrayAdapter methods. Use the methods on the ArrayAdapter object. Add, Insert, Remove, Clear for example. This should dynamically update the list.
You can also use OnNotifyDataSetChanged, but the best practice would be to use the ArrayAdapter's methods to update the list.
Hello @FilippoBiondi
looks at these article
https://forums.xamarin.com/discussion/39355/how-to-add-a-another-item-to-listview-on-button-click-event
https://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_2_-_populating_a_listview_with_data/
have a nice coding day
Hi @FilippoBiondi
public class Person { public string name {get;set;} }
private void addPerson_click(object sender, EventArgs e) { Person person=new Person(){name="Mabrouk"}; // or get data from an other way... listView.ItemsSource.Add(person); }
<ListView x:Name="listView" VerticalOptions="FillAndExpand" SeparatorVisibility="None"> <ListView.ItemTemplate> <DataTemplate> <Label Text="{Binding name}" /> </DataTemplate> </ListView.ItemTemplate> </ListView>
Hope that helps.
Mabrouk.
If you are working in Xamarin.Forms, @Mabrouk has the correct answer. If you are working in Xamarin.Android, you'll need to work with the array adapter object.
@JBeck , yes my answer was for Xamarin.Forms..
it will be better when you customize your Adapter..
But in xamarin.android, @FilippoBiondi needs to something like this :
1. having a
private List<Person> _persons=new List<Person>();
before,2. Add the new person to
_persons
in the button click event like :private void addPerson_click(object sender, EventArgs e) { Person person=new Person(){name="Mabrouk"}; // or get data from an other way... _persons.Add(person); ArrayAdapter adapter = new ArrayAdapter (this, Resource.Layout.TextViewItem, _persons); listView.Adapter=adapter; }
3. Note : the code is not perfect
Hi @Mabrouk
Your code will work, but it will result in rebuilding the list every time the user clicks the add button, which is very inefficient.
In the following code, the button click event adds the code to the Adapter's existing list. Note that I've added the ListViewItem.axml to the Resources/layout folder, with a TextView with ID label. I've attached a screenshot of it in action.
MainActivity
Main.axml
ListViewItem.axml
Friend @JBeck ,
Yes, thats why i made a note that : the code is not perfect