Search This Blog

Sunday, 10 July 2016

SQLite Example in Xamarin

LoginAdopter.CS:-

using System;
using Android.Widget;
using System.Collections.Generic;
using Android.App;
using Android.Views;
namespace SQLiteExample
{
public class LoginAdapter:BaseAdapter
{
List<Registration> ObjRegistration;
Activity act;
public LoginAdapter (Activity act,List<Registration> ObjRegistration)
{
this.act = act;
this.ObjRegistration = ObjRegistration;
}
public override Java.Lang.Object GetItem (int position)
{
return position;
}
public override long GetItemId (int position)
{
return position;
}
public override Android.Views.View GetView (int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
View rootView = act.LayoutInflater.Inflate (Resource.Layout.LayoutItem, parent, false);
TextView tvUsername = rootView.FindViewById<TextView> (Resource.Id.tvUsername);
TextView tvPassword= rootView.FindViewById<TextView> (Resource.Id.tvPassword);
Registration item = ObjRegistration [position];
tvUsername.Text = item.UserName;
tvPassword.Text = item.Password;
return rootView;
}
public override int Count {
get {
return ObjRegistration.Count;
}
}
}
}


App.CS:-

using System;
using SQLite;

namespace SQLiteExample
{
public class App
{
public App ()
{
}

public static SQLiteConnection conn;
}
}
Main.AXML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter User Name" />
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter password" />
    <Button
        android:id="@+id/btnSubmit"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />
    <Button
        android:id="@+id/login"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="login Page" />
    <ListView
        android:id="@+id/lstLoginDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


MainActivity.CS:-

using Android.App;
using Android.Widget;
using SQLite;
using System;
using Android.OS;
using Android.Content;
using System.Collections.Generic;
using System.Linq;
namespace SQLiteExample
{
[Activity (Label = "SQLiteExample", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
CreateDatabase ();
Button btnSubmit = (Button)FindViewById (Resource.Id.btnSubmit);
Button login = (Button)FindViewById (Resource.Id.login);
login.Click += async delegate(object sender, EventArgs e) {
Intent objIntent=new Intent(this,typeof(LoginPage));
StartActivity(objIntent);
};
EditText etUserName = (EditText)FindViewById (Resource.Id.etUserName);
EditText etPassword = (EditText)FindViewById (Resource.Id.etPassword);
btnSubmit.Click += async delegate(object sender, EventArgs e) {
Registration objRegister=new Registration();
objRegister.Id=1;
objRegister.UserName=etUserName.Text;
objRegister.Password=etPassword.Text;
int i= App.conn.Insert(objRegister);
if(i==1){
Toast.MakeText(this,"Successfully Inserted",ToastLength.Long).Show();
}else{
Toast.MakeText(this,"some thing went wrong. Please reinsert once.",ToastLength.Long).Show();
}
List<Registration> results=App.conn.Table<Registration>().ToList();
ListView logindetails = (ListView)FindViewById(Resource.Id.lstLoginDetails);
LoginAdapter ObjLoginAdapter = new LoginAdapter(this,results);
logindetails.Adapter=ObjLoginAdapter;
};
}
private void CreateDatabase(){
string folder = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
App.conn = new SQLiteConnection (System.IO.Path.Combine (folder, "PersonData.db"));
App.conn.CreateTable<Registration>();
}
}
}

LoginLayout.AXML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter User Name" />
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter password" />
    <Button
        android:id="@+id/btnLogin"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />
</LinearLayout>

LoginPage.CS:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;
namespace SQLiteExample
{
[Activity (Label = "LoginPage")]
public class LoginPage : Activity
{
SQLiteConnection con;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.LoginLayout);
Button btnLogin = (Button)FindViewById (Resource.Id.btnLogin);
EditText etUserName = (EditText)FindViewById (Resource.Id.etUserName);
EditText etPassword = (EditText)FindViewById (Resource.Id.etPassword);
btnLogin.Click += async delegate(object sender, EventArgs e) {
bool isSuccess=false;
List<Registration> results=App.conn.Table<Registration>().ToList();
// foreach(var eachItem in results){
// if(eachItem.UserName==etUserName.Text&&eachItem.Password==etPassword.Text){
// Intent objIntent=new Intent(this,typeof(LoginPage));
// StartActivity(objIntent);
// break;
// isSuccess=true;
// }else{
// isSuccess=false;
//
// }
};
}
}
}
Registration.CS:-

using System;
using SQLite;

namespace SQLiteExample
{
public class Registration
{
public Registration ()
{
}

[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
}

Layoutitem.AXML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />
</LinearLayout>

No comments:

Post a Comment