forked from ZhangGaoxing/xamarin-android-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15e06e1
commit a96dc8a
Showing
19 changed files
with
770 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.27130.2026 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteDemo", "SqliteDemo\SqliteDemo.csproj", "{06A73000-DB5D-42F9-BA5F-A1978003AA4E}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU | ||
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Release|Any CPU.Deploy.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {CEF344A6-F7AB-4726-8587-FC793454C218} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 SqliteDemo.Managers; | ||
using SqliteDemo.Models; | ||
|
||
namespace SqliteDemo.Acitvities | ||
{ | ||
[Activity(Label = "AddActivity")] | ||
public class AddActivity : Activity | ||
{ | ||
protected override void OnCreate(Bundle savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
SetContentView(Resource.Layout.AddLayout); | ||
|
||
Button add = FindViewById<Button>(Resource.Id.add_btn_add); | ||
EditText name = FindViewById<EditText>(Resource.Id.add_edit_name); | ||
EditText sex = FindViewById<EditText>(Resource.Id.add_edit_sex); | ||
EditText age = FindViewById<EditText>(Resource.Id.add_edit_age); | ||
|
||
add.Click += (s, e) => | ||
{ | ||
SqliteManager<Person> sqlite = new SqliteManager<Person>(); | ||
|
||
sqlite.Insert(new Person | ||
{ | ||
Name = name.Text, | ||
Sex = sex.Text, | ||
Age = Convert.ToInt32(age.Text) | ||
}); | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 SqliteDemo.Managers; | ||
using SqliteDemo.Models; | ||
|
||
namespace SqliteDemo.Acitvities | ||
{ | ||
[Activity(Label = "DeleteActivity")] | ||
public class DeleteActivity : Activity | ||
{ | ||
protected override void OnCreate(Bundle savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
SetContentView(Resource.Layout.DeleteLayout); | ||
|
||
Button del = FindViewById<Button>(Resource.Id.del_btn_del); | ||
EditText id = FindViewById<EditText>(Resource.Id.del_edit_id); | ||
|
||
del.Click += (s, e) => | ||
{ | ||
SqliteManager<Person> sqlite = new SqliteManager<Person>(); | ||
|
||
sqlite.DbConnection.Execute($"delete from Person where id = '{id.Text}'"); | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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 SqliteDemo.Managers; | ||
using SqliteDemo.Models; | ||
|
||
namespace SqliteDemo.Acitvities | ||
{ | ||
[Activity(Label = "QueryActivity")] | ||
public class QueryActivity : Activity | ||
{ | ||
protected override void OnCreate(Bundle savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
SetContentView(Resource.Layout.QueryLayout); | ||
|
||
TextView res = FindViewById<TextView>(Resource.Id.que_text_res); | ||
|
||
SqliteManager<Person> sqlite = new SqliteManager<Person>(); | ||
var list = sqlite.QueryAll(); | ||
foreach (var item in list) | ||
{ | ||
res.Text += $"ID:{item.ID} Name:{item.Name} Sex:{item.Sex} Age:{item.Age}\n"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Any raw assets you want to be deployed with your application can be placed in | ||
this directory (and child directories) and given a Build Action of "AndroidAsset". | ||
|
||
These files will be deployed with you package and will be accessible using Android's | ||
AssetManager, like this: | ||
|
||
public class ReadAsset : Activity | ||
{ | ||
protected override void OnCreate (Bundle bundle) | ||
{ | ||
base.OnCreate (bundle); | ||
|
||
InputStream input = Assets.Open ("my_asset.txt"); | ||
} | ||
} | ||
|
||
Additionally, some Android functions will automatically load asset files: | ||
|
||
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Android.App; | ||
using Android.Widget; | ||
using Android.OS; | ||
using Android.Content; | ||
using SqliteDemo.Acitvities; | ||
using SqliteDemo.Managers; | ||
using SqliteDemo.Models; | ||
|
||
namespace SqliteDemo | ||
{ | ||
[Activity(Label = "SqliteDemo", MainLauncher = true, Theme = "@android:style/Theme.Material.Light")] | ||
public class MainActivity : Activity | ||
{ | ||
protected override void OnCreate(Bundle savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
SetContentView(Resource.Layout.Main); | ||
|
||
Button add = FindViewById<Button>(Resource.Id.main_btn_add); | ||
Button del = FindViewById<Button>(Resource.Id.main_btn_del); | ||
Button que = FindViewById<Button>(Resource.Id.main_btn_que); | ||
|
||
add.Click += (s, e) => | ||
{ | ||
Intent dialog = new Intent(this, typeof(AddActivity)); | ||
StartActivity(dialog); | ||
}; | ||
del.Click += (s, e) => | ||
{ | ||
Intent dialog = new Intent(this, typeof(DeleteActivity)); | ||
StartActivity(dialog); | ||
}; | ||
que.Click += (s, e) => | ||
{ | ||
Intent dialog = new Intent(this, typeof(QueryActivity)); | ||
StartActivity(dialog); | ||
}; | ||
|
||
SqliteManager<Person> sqlite = new SqliteManager<Person>(); | ||
sqlite.CreateTable(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
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.Net; | ||
using SQLite.Net.Platform.XamarinAndroid; | ||
|
||
namespace SqliteDemo.Managers | ||
{ | ||
class SqliteManager<T> where T : class | ||
{ | ||
/// <summary> | ||
/// 数据库文件路径 | ||
/// </summary> | ||
private string dbPath = string.Empty; | ||
|
||
public string DbPath | ||
{ | ||
get | ||
{ | ||
if (string.IsNullOrEmpty(dbPath)) | ||
{ | ||
dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "info.db3"); | ||
} | ||
|
||
return dbPath; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 数据库连接 | ||
/// </summary> | ||
public SQLiteConnection DbConnection | ||
{ | ||
get | ||
{ | ||
return new SQLiteConnection(new SQLitePlatformAndroid() ,DbPath); | ||
} | ||
} | ||
|
||
public SqliteManager() | ||
{ | ||
|
||
} | ||
|
||
public SqliteManager(string dbPath) | ||
{ | ||
this.dbPath = dbPath; | ||
} | ||
|
||
/// <summary> | ||
/// 创建表 | ||
/// </summary> | ||
public void CreateTable() | ||
{ | ||
using (var db = DbConnection) | ||
{ | ||
var c = db.CreateTable<T>(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 插入数据 | ||
/// </summary> | ||
/// <param name="item">插入值</param> | ||
public void Insert(T item) | ||
{ | ||
using (var db = DbConnection) | ||
{ | ||
db.Insert(item); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 插入数据 | ||
/// </summary> | ||
/// <param name="item">插入值</param> | ||
public void InsertAll(IEnumerable<T> list) | ||
{ | ||
using (var db = DbConnection) | ||
{ | ||
db.InsertAll(list); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 删除数据 | ||
/// </summary> | ||
/// <param name="item">删除值</param> | ||
public void Delete(T item) | ||
{ | ||
using (var db = DbConnection) | ||
{ | ||
db.Delete(item); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 更新数据 | ||
/// </summary> | ||
/// <param name="item">更新值</param> | ||
public void Update(T item) | ||
{ | ||
using (var db = DbConnection) | ||
{ | ||
db.Update(item); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 查询所有数据 | ||
/// </summary> | ||
/// <returns>表中所有数据</returns> | ||
public List<T> QueryAll() | ||
{ | ||
using (var db = DbConnection) | ||
{ | ||
return db.Table<T>().ToList(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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.Net.Attributes; | ||
|
||
namespace SqliteDemo.Models | ||
{ | ||
public class Person | ||
{ | ||
[PrimaryKey, AutoIncrement] | ||
public int ID { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string Sex { get; set; } | ||
|
||
public int Age { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="SqliteDemo.SqliteDemo" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
<uses-sdk android:minSdkVersion="21" /> | ||
<application android:allowBackup="true" android:label="@string/app_name"> | ||
</application> | ||
</manifest> |
Oops, something went wrong.