简体   繁体   中英

Deleting from database by clicking button

I am creating an app using the SqlLite database. I am able to populate the database with some basic information from the sign-up page and display only the ID of the person under the Login tab (This is for testing purposes for now). How would I go about deleting a specific position in the database by clicking the delete button? here is my cs where I am connecting to my database and using it to display just the Id on the page under a list view.

namespace SP
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.FilePath))
            {
                conn.CreateTable<SignInInformation>();
                var info = conn.Table<SignInInformation>().ToList();

                userData.ItemsSource = info;
            }
        }

        void DeleteButton_Clicked(object sender, EventArgs e)
        {
            
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SP.LoginPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="DeleteButton"
                     Text="Delete"
                     Clicked="DeleteButton_Clicked">
        </ToolbarItem>
    </ContentPage.ToolbarItems>
    
    
    <ContentPage.Content>
        <ListView x:Name="userData">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Id}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

How would I go about deleting a specific position in the database by clicking the delete button?

Yu can use Xamarin.Forms MenuItem to achieve this function.

The Xamarin.Forms MenuItem class defines menu items for menus such as ListView item context menus and Shell application flyout menus.

You can refer to the following code:

<ListView x:Name="listView" Margin="20" ItemSelected="OnListItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Margin="20,0,0,0" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding Name}" VerticalTextAlignment="Center" HorizontalOptions="StartAndExpand" />
                    <Image Source="check.png" HorizontalOptions="End" IsVisible="{Binding Done}" />
                </StackLayout>

                <ViewCell.ContextActions>
                    <MenuItem Text="Delete"
                                  Clicked="OnDeleteClicked"/>
                </ViewCell.ContextActions>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Method OnDeleteClicked in TestPage.xaml.cs

    async void OnDeleteClicked(object sender, EventArgs e)
    {
        TodoItem itemToDelete = ((sender as MenuItem).BindingContext as TodoItem);

        TodoItemDatabase database = await TodoItemDatabase.Instance;
        await database.DeleteItemAsync(itemToDelete);
    }

For more details, check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/menuitem .

And there is also a sample included in above doc, you can check it here: https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-menuitemdemos/ .

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