Hello everyone. I am trying to delete listview item. The item it self deletes from the database fine but the list view doesn't update, I am even using an observableCollection. This is my code:
`public partial class MainPage : ContentPage
{
DatabaseCRUDOperations dcr = new DatabaseCRUDOperations();
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
listUsers.ItemsSource = dcr.GetUsers();
}
private void Delete_Clicked(object sender, EventArgs e)
{
var user = (sender as MenuItem).CommandParameter as UserModel;
dcr.DeleteMember(user.ID);
}
private void Handle_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new UserRegistrationView());
}
}`
This is the Xaml file for mainpage:
` <ContentPage.ToolbarItems>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<ListView.ItemTemplate>
<TextCell.ContextActions>
</TextCell.ContextActions>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>`
`public class DatabaseCRUDOperations
{
//the connection
private static SQLiteConnection _connection;
//Create database
public DatabaseCRUDOperations()
{
//get the connection for independent platform
_connection = DependencyService.Get<SQLiteDb>().GetConnection();
//create table in database
_connection.CreateTable<UserModel>();
}
//Get the users from the database
public ObservableCollection<UserModel> GetUsers()
{
// IEnumerable<UserModel> user = _connection.Table<UserModel>().ToList();
ObservableCollection<UserModel> user = new ObservableCollection<UserModel>(_connection.Table<UserModel>().ToList());
return user;
}
//Add member to the database
public string AddMember(UserModel _user)
{
//Access the DB and add the user
_connection.Insert(_user);
return "Successfully added member!";
}
//Delete user from the database
public string DeleteMember(int id)
{
//Access the DB and delete the user at this ID
_connection.Delete<UserModel>(id);
return "Successfully deleted member!";
}
}`
` [Table("UserModel")]
public class UserModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}`
public interface SQLiteDb { SQLiteConnection GetConnection(); }
` public partial class UserRegistrationView : ContentPage, INotifyPropertyChanged
{
public DatabaseCRUDOperations userDatabase;
public UserModel user;
public UserRegistrationView ()
{
InitializeComponent ();
}
public void Save_Clicked(object o, EventArgs e)
{
user = new UserModel
{
FirstName = fname.Text,
LastName = lname.Text
};
userDatabase = new DatabaseCRUDOperations();
userDatabase.AddMember(user);
}
}`