简体   繁体   中英

String not Added to Observable Collection Xamarin Forms

I have created an observable collection that is updating correctly. But is only updating one of the strings in the observable collection. I have thrown a debug point in loop that is doing this and the list of strings is showing as having two items but only one of them gets added to the observable collection.

The function that sets the observable collection:

public async Task GetNotificationData()
    {
       List<string> intermediary =  _database.getMessages();

        foreach (var variable in intermediary)
        {
            DataBaseList = new ObservableCollection<string> { variable };
        }
        _DataBaseList = DataBaseList;
    }

and the function (getMessages) that returns the data from an SQLite database:

   public List<string> getMessages()
    {

        var DataBaseSelection =  _connection.Query<MessageTable>("Select * From [MessageTable]");
        List<string> dataList = new List<string>();

        for (var i=0;i<DataBaseSelection.Count;i++)
        {
            dataList.Add(DataBaseSelection[i].message);
        }
        return dataList;

    }

And for completeness the Xaml:

<ListView
    x:Name="notificationList"
    ItemsSource="{Binding DataBaseList}">
</ListView>

Just to be clear this is showing one of the strings in the list on the front end but not the other, like its skipping the second string in the list. The first string in the list is "test" the second one is called "test2" but only test is showing in the list view.

Any insights?

In your foreach, you create a new instance of the ObservableCollection .

foreach (var variable in intermediary)
{
    DataBaseList = new ObservableCollection<string> { variable };
}

For a ObservableCollection to work correctly, you need to instantiate it once. Just clear the list if you want to add new ones, like this:

DataBaseList.Clear();

foreach (var variable in intermediary)
{
    DataBaseList.Add(variable);
}

Sorry for the poor english.

In this piece of code that sets the observable collection, you are overwriting the content of DataBaseList in each iteration, and in the end you are setting just the last result of this to the observable collection _DataBaseList .

foreach (var variable in intermediary)
{
    DataBaseList = new ObservableCollection<string> { variable };
}
_DataBaseList = DataBaseList;

What I suggest you do: - Initialize your observable collection at the constructor; - To fill it, use:

_DataBaseList.Clear();
foreach(var variable in intermediary)
{
    _DatabaseList.Add(variable);
}

And this must works. Check it out.

There is two approach for using an empty ObservableColletion :

  1. Instance the ObservableColletion only once, and then use Clear() to remove all elements.
  2. Instance a new ObservableCollection that implements the INotifyPropertyChanged interface.

Your error is due to the ObservableCollection implements INotifyCollectionChanged but it doesn't implement the INotifyPropertyChanged interface.

I hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM