ListView Update when underlying data changed (sqlite)

durpexdurpex DEMember

Hi,

I have a ListView that has its DataSource set to the result of a sqlite query as described here.
Basically I am doing something like this:

public List<LocalData> getLocalData()
{
return conn.Table<LocalData>().OrderBy(x => x.ID).ToList<LocalData>();
}
listView.ItemsSource = db.getLocalData();

Now I am in the situation where data is added to the database and I need to update my listView.
Currently I do it by querying the database and setting the ItemSource to the new result.
This is however to slow.
How could I modify my structure so that whenever there is a change in the db (new row, deleted row, modified field) the listView automatically
reflects it without manually updating everything?
Searching the documentation it seems that I have to implement INotifyPropertyChanged, but just implementing it within LocalData does not do anything.
Do I have to update the way I insert data into the db? I currently just do a

 conn.Insert(newLocalData);

Any ideas or small working samples?

Regards,
durpex

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Fist of all you should have an ObservableCollection.

    myObservableCollection = db.getLocalData();
    
    listView.ItemSource = myObservableCollection;
    

    Then you should change your Collection AND your Sqlite

    myObservableCollection.Add(newLocalData);
    conn.Insert(newLocalData);
    
  • durpexdurpex DEMember

    @AlessandroCaliaro said:
    Fist of all you should have an ObservableCollection.

    myObservableCollection = db.getLocalData();

    listView.ItemSource = myObservableCollection;

    Then you should change your Collection AND your Sqlite

    myObservableCollection.Add(newLocalData);
    conn.Insert(newLocalData);

    So I have to manually make sure to update/modify the ObservableCollection every time I perform something within the database (create, update, delete,...)? There is no automatic "binding" of the two? They should always hold the same data.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I am not sure about it... I think SQLite does not have this possibility

Sign In or Register to comment.