🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Xamarin / Xamarin Forms SQLite ile basit ekle sil güncelle ve listview kullanımı

1-) Xamarin - Xamarin Forms SQLite ile basit ekle sil güncelle ve listview kullanımı

 

 

 

 

NuGet'ten şunları yükle -> "Plugin.Toast" ve  "sqlite-net-pcl"

 

Models -> OgrenciDatabase.cs

 

 

using SQLite;

using System.Collections.Generic;

using System.Threading.Tasks;

 

namespace App_Final2.Models

{

    public class OgrenciDatabase

    {

        readonly SQLiteAsyncConnection _database;

 

        public OgrenciDatabase(string dbPath)

        {

            _database = new SQLiteAsyncConnection(dbPath);

            _database.CreateTableAsync<Ogrenci>().Wait();

        }

        public Task<List<Ogrenci>> GetOgrencisAsync()

        {

            return _database.Table<Ogrenci>().ToListAsync();

        }

        public Task<Ogrenci> GetOgrenciAsync(int id)

        {

            return _database.Table<Ogrenci>()

                            .Where(i => i.id == id)

                            .FirstOrDefaultAsync();

        }

        public Task<int> SaveOgrenciAsync(Ogrenci ogrenci)

        {

            if (ogrenci.id != 0)

            {

                return _database.UpdateAsync(ogrenci);

            }

            else

            {

                return _database.InsertAsync(ogrenci);

            }

        }

        public Task<int> DeleteOgrenciAsync(Ogrenci ogrenci)

        {

            return _database.DeleteAsync(ogrenci);

        }

    }

}

 

 

Models -> Ogrenci.cs

 

using SQLite;

 

namespace App_Final2.Models

{

    public class Ogrenci

    {

        [PrimaryKey, AutoIncrement]

        public int id { get; set; }

        public string ad { get; set; }

        public string soyad { get; set; }

    }

}

 

App.xaml.cs

 

using App_Final2.Models;

using System;

using System.IO;

using Xamarin.Forms;

using Xamarin.Forms.Xaml;

 

namespace App_Final2

{

    public partial class App : Application

    {

        static OgrenciDatabase database;

 

        public static OgrenciDatabase Database

        {

            get

            {

                if (database == null)

                {

                    database = new OgrenciDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Ogrencis.db3"));

                }

                return database;

            }

        }

 

        public App()

        {

            InitializeComponent();

 

            MainPage = new MainPage();

        }

 

        protected override void OnStart()

        {

        }

 

        protected override void OnSleep()

        {

        }

 

        protected override void OnResume()

        {

        }

    }

}

 

MainPage.xaml

 

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:d="http://xamarin.com/schemas/2014/forms/design"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             mc:Ignorable="d"

             x:Class="App_Final2.MainPage">

 

    <StackLayout Margin="20">

        <Entry x:Name="txtAd" Placeholder="Ad" PlaceholderColor="Olive" />

        <Entry x:Name="txtSoyad" Placeholder="Soyad" PlaceholderColor="Olive" />

        <Entry x:Name="txtId" Placeholder="İd" PlaceholderColor="Olive" />

 

        <RelativeLayout HorizontalOptions="FillAndExpand">

            <Button Text="Ekle" RelativeLayout.XConstraint="{ConstraintExpression

            Type=RelativeToParent,Property=Width,Factor=.0000,Constant=0}"

            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,

            Property=Width,Factor=.3333,Constant=0}"

                    Clicked="EkleClick"

                    />

            <Button Text="Sil" RelativeLayout.XConstraint="{ConstraintExpression

            Type=RelativeToParent,Property=Width,Factor=.3333,Constant=0}"

            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,

            Property=Width,Factor=.3333,Constant=0}"

                     Clicked="SilClick"

                    />

            <Button Text="Güncelle" RelativeLayout.XConstraint="{ConstraintExpression

            Type=RelativeToParent,Property=Width,Factor=.6666,Constant=0}"

            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,

            Property=Width,Factor=.3333,Constant=0}"

                     Clicked="GuncelleClick"

                    />

        </RelativeLayout>

 

        <ListView x:Name="listView" HasUnevenRows="True" SelectedItem="{Binding Ogrenci}" ItemSelected="listView_ItemSelected"   >

            <ListView.ItemTemplate >

                <DataTemplate>

                    <ViewCell>

                        <StackLayout Orientation="Horizontal">

                            <Label Text="{Binding ad}" FontSize="Large" />

                            <Label Text="{Binding soyad}" FontSize="Large"/>

                            <Label Text="{Binding id}" FontSize="Large"/>

                        </StackLayout>

                    </ViewCell>

                </DataTemplate>

            </ListView.ItemTemplate>

        </ListView>

 

    </StackLayout>

 

</ContentPage>

 

 

MainPage.xaml.cs

 

using App_Final2.Models;

using Plugin.Toast;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Xamarin.Forms;

 

namespace App_Final2

{

    // Learn more about making custom code visible in the Xamarin.Forms previewer

    // by visiting https://aka.ms/xamarinforms-previewer

    [DesignTimeVisible(false)]

    public partial class MainPage : ContentPage

    {

        public MainPage()

        {

            InitializeComponent();

            OnAppearing();

        }

 

 

        protected override async void OnAppearing()

        {

            base.OnAppearing();

 

            listView.ItemsSource = await App.Database.GetOgrencisAsync();

 

         

        }

 

        async void EkleClick(object sender, EventArgs e)

        {

            try

            {

 

                var ogrenci = new Ogrenci();

                ogrenci.ad = txtAd.Text;

                ogrenci.soyad = txtSoyad.Text;

                await App.Database.SaveOgrenciAsync(ogrenci);

                //await Navigation.PopAsync();

 

                OnAppearing();

                CrossToastPopUp.Current.ShowToastMessage("Eklendi");

            }

            catch (Exception ex)

            {

                CrossToastPopUp.Current.ShowToastMessage("Hata "+ex.Message);

            }

        }

 

        async void SilClick(object sender, EventArgs e)

        {

            try

            {

                Ogrenci seciliOgrenci =(Ogrenci) listView.SelectedItem;

 

                var ogrenci = new Ogrenci();

                ogrenci.id = seciliOgrenci.id;

                await App.Database.DeleteOgrenciAsync(ogrenci);

                //await Navigation.PopAsync();

 

 

                OnAppearing();

                CrossToastPopUp.Current.ShowToastMessage("Silindi");

 

            }

            catch (Exception ex)

            {

                CrossToastPopUp.Current.ShowToastMessage("Hata " + ex.Message);

            }

        }

 

        async void GuncelleClick(object sender, EventArgs e)

        {

            try

            {

 

                Ogrenci seciliOgrenci = (Ogrenci)listView.SelectedItem;

 

                var ogrenci = new Ogrenci();

                ogrenci.id = seciliOgrenci.id;

                ogrenci.ad = txtAd.Text;

                ogrenci.soyad = txtSoyad.Text;

                await App.Database.SaveOgrenciAsync(ogrenci);

                //await Navigation.PopAsync();

 

 

                OnAppearing();

                CrossToastPopUp.Current.ShowToastMessage("Güncellendi");

            }

            catch (Exception ex)

            {

                CrossToastPopUp.Current.ShowToastMessage("Hata " + ex.Message);

            }

        }

 

        private void listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)

        {

            Ogrenci seciliOgrenci = (Ogrenci)listView.SelectedItem;

            txtAd.Text = seciliOgrenci.ad;

            txtSoyad.Text = seciliOgrenci.soyad;

            txtId.Text = seciliOgrenci.id.ToString();

        }

    }

}

 

 

 

 

EKRAN GÖRÜNTÜSÜ



kaynak kodlar : https://drive.google.com/file/d/1sMDFsIvy_5au4WtP6psUhH9sw4a6tS_7/view

kısa yapılış videosu : https://www.youtube.com/watch?v=4H87RKCHAXI&feature=youtu.be&ab_channel=ramazanhaber

apk : https://drive.google.com/open?id=1pxi6GafLQj8ni5YJu0lQRDkq7dwb0VzL

 

 

 2021 Ocak 21 Perşembe
 563