Ajay Nair
Ajay Nair

Reputation: 65

how to dynamically update listview based on values of class

I am developing a contact book application wherein I have a listview with a add button. On clicking the add button it redirects the user to a modal page asking for it's name, email id, institution. All of this details are saved in a ContactDetails class. On clicking done button on the modal page I want to display the contents in my listview.

Any suggestions on how to implement this successfully.

Code am currently working on are as follows: ContactDetails.cs

public class ContactDetails
{
    public string Name { get; set; }
    public string Inst { get; set; }
    public string EmailId { get; set;}  
    public int Mob { get; set;}
}

ContactDetailsModalPage.xaml.cs

public partial class ContactDetailsModalPage: ContentPage
{
    CandidateDetails cd = new CandidateDetails();
    async void OnDoneClicked(object sender, System.EventArgs e)
    {
        cd.Name = (string)candNameEntry.Text; 
        cd.Inst = (string)candInst.Text;
        cd.EmailId = (string)candEmailId.Text;
        cd.Mob = Convert.ToInt32(candMobNumber.Text);

        List<ContactDetails> candList = new List<ContactDetails>();
        candList.Add(cd);

        await Navigation.PopModalAsync();

    }


    public CandidateDetailsModalPage()
    {
        InitializeComponent();

    }
}

I am not able to understand as to what should I provide as the itemsource for my listview so that it can dynamically display the value that are stored in the ContactDetails class by modal page.

Upvotes: 0

Views: 486

Answers (2)

woelliJ
woelliJ

Reputation: 1504

I'm afraid you're giving us too little information.

  1. Are you using the MVVM pattern? If not: You need a reference to the ListView and add a new Cell. If yes: Please post your ViewModel holding the bound ContactDetails List.
  2. Like @Jason said, if there is a List of ContactDetails that is bound to the ItemsSourceof the ListView - it needs to be an ObservableCollection<ContactDetails>. All you have do do in your ClickHandler is to get a reference to that List and add / insert the new item.

Upvotes: 1

Jason
Jason

Reputation: 89082

You need to use an ObservableCollection instead of a List. An ObservableCollection will notify any data bound element of changes.

Upvotes: 1

Related Questions