Q:

UWP - Display database in listview with certain conditions

I have a database called "Tryout.sqlite". I want to display the sqlite in a listview. If the user clicks on "kelas 7", then the one displayed in the listview has only "kelas 7" questions.
Database:

Code:
string path;
SQLite.Net.SQLiteConnection conn;
List<DBName> DB_TryoutList = new List<DBName>();

kelasDetail = e.ClickedItem as Kelas;
path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Tryout.sqlite");
conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);

if (kelasDetail.KelasList == "Kelas 7")
{
    DB_TryoutList.Clear();
    var query = conn.Table<DBName>();
    string name = "";

    foreach (var message in query)
    {
        if (message.Judul.ToLower().Contains("kelas 7 -") || message.Judul.ToLower().Contains("kelas 7-"))
        {
            name = message.Judul;

            this.DB_TryoutList.Add(new DBName { ID = message.ID, Judul = name, Deskripsi = message.Deskripsi, Durasi = message.Durasi, JumlahSoal = message.JumlahSoal, Syarat = message.Syarat });
        }
     }
}

else if (kelasDetail.KelasList == "Kelas 9")
{
    DB_TryoutList.Clear();
    var query = conn.Table<DBName>();
    string name = "";

    foreach (var message in query)
    {
        if (message.Judul.ToLower().Contains("kelas 9 -") || message.Judul.ToLower().Contains("kelas 9-"))
        {
            name = message.Judul;

            this.DB_TryoutList.Add(new DBName { ID = message.ID, Judul = name, Deskripsi = message.Deskripsi, Durasi = message.Durasi, JumlahSoal = message.JumlahSoal, Syarat = message.Syarat });
        }
    }
}

if (DB_TryoutList.Count > 0)
{
    ListTryout.Visibility = Visibility.Visible;
    this.ListTryout.ItemsSource = DB_TryoutList;
}

I have a problem, which is when I click "kelas 7", the problem shown is "kelas 7", and after that I click "kelas 9", the problem is displayed as still in "kelas 7" (as shown below)

How to handle it?

A:

I think the problem is that you are using a List<DBName> instead of ObservableCollection<DBName>. You do Clear and Add new items in the list when user clicks, but because the ListView is already bound to the same instance, assigning it to ItemsSource is a no-op. To fix this you either have to switch to using ObservableCollection<DBName>, because then the ListView will be notified about any changes in the collection, or you have to use a new instance of List<DBName> after each click.

