日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

app把信息添加到mysql_如何将数据库表中的数据添加到ListView C#Xamarin Android App

發(fā)布時間:2024/10/8 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 app把信息添加到mysql_如何将数据库表中的数据添加到ListView C#Xamarin Android App 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

幾天前我問過如何在活動之間共享數(shù)據(jù),一個用戶告訴我使用SQLite,所以我做了.我想讓用戶點擊MainLayout中的按鈕,它會將他重定向到AddTaskLayout,在那里他可以添加任務(wù)名稱,按下Save按鈕應(yīng)用程序會將他重定向回MainLayout,他的任務(wù)將在ListView中列出.

到目前為止,我創(chuàng)建了數(shù)據(jù)庫,表格和我需要的一切.我的問題是:如何將存儲在數(shù)據(jù)庫表中的數(shù)據(jù)添加到ListView?我發(fā)現(xiàn)的每個答案都是用Java編寫的,所以搜索舊的StackOverflow問題并沒有那么有用:/

這是代碼:

我的DBRepository是表示創(chuàng)建數(shù)據(jù)庫,創(chuàng)建表,向表插入數(shù)據(jù)以及獲取相同數(shù)據(jù)的類:

public class DBRepository

{

public void CreateDatabase()

{

string dbPath = Path.Combine(System.Environment.GetFolderPath

(System.Environment.SpecialFolder.Personal), "database.db3");

var db = new SQLiteConnection(dbPath);

}

public void CreateTable()

{

string dbPath = Path.Combine(System.Environment.GetFolderPath

(System.Environment.SpecialFolder.Personal), "database.db3");

var db = new SQLiteConnection(dbPath);

db.CreateTable();

}

public string InsertRecord(string task)

{

string dbPath = Path.Combine(System.Environment.GetFolderPath

(System.Environment.SpecialFolder.Personal), "database.db3");

var db = new SQLiteConnection(dbPath);

ToDoTasks item = new ToDoTasks();

item.Task = task;

db.Insert(item);

return task;

}

public string GetData()

{

string dbPath = Path.Combine(System.Environment.GetFolderPath

(System.Environment.SpecialFolder.Personal), "database.db3");

var db = new SQLiteConnection(dbPath);

string output = "";

var table = db.Table();

foreach(var item in table)

{

output += item;

}

return output;

}

}

我創(chuàng)建表的ToDoTasks類:

[Table("ToDo")]

public class ToDoTasks

{

[PrimaryKey, AutoIncrement, Column("_Id")]

public int Id { get; set; }

[MaxLength(100)]

public string Task { get; set; }

}

我的AddTaskActivity表示用戶輸入任務(wù)名稱的第二個布局:

protected override void OnCreate(Bundle bundle)

{

base.OnCreate(bundle);

SetContentView(Resource.Layout.AddTask);

//define buttons

Button save, cancel;

save = FindViewById(Resource.Id.save);

cancel = FindViewById(Resource.Id.cancel);

save.Click += save_click;

cancel.Click += cancel_click;

}

private void save_click(object sender, EventArgs e)

{

DBRepository dbr = new DBRepository();

EditText name = FindViewById(Resource.Id.taskName);

//enter user's input(task name) to table

var result = dbr.InsertRecord(name.Text);

StartActivity(typeof(MainActivity));

}

private void cancel_click(object sender, EventArgs e)

{

StartActivity(typeof(MainActivity));

}

我想要填充listView的MainActivity:

protected override void OnCreate(Bundle bundle)

{

base.OnCreate(bundle);

// Set view

SetContentView(Resource.Layout.Main);

//create database if it doesn't exist

DBRepository dbr = new DBRepository();

dbr.CreateDatabase();

//create table (if it doesn't exist)

dbr.CreateTable();

//Define buttons

Button addTask;

ListView list;

addTask = FindViewById(Resource.Id.addTask);

addTask.Click += addTask_click;

}

private void addTask_click(object sender, EventArgs e)

{

StartActivity(typeof(AddTaskActivity));

}

我非常感謝你的幫助.我知道這些都是非常基本的問題,但有人必須要求他們?yōu)樽约汉驮S多其他(未來)C#android開發(fā)人員.謝謝!

//

更新:我檢查了Johan的答案是否正確,但這是(在我的情況下)正確的代碼:

我需要更改GetData()方法以返回List(不像以前那樣對象),然后在ListView中顯示List.這是代碼:

You helped me a lot, but I had to make few changes, so here they are for the record:

在DBRepository中需要將GetData()方法更改為:

public List GetData()

{

string dbPath = Path.Combine(System.Environment.GetFolderPath

(System.Environment.SpecialFolder.Personal), "database.db3");

var db = new SQLiteConnection(dbPath);

List data = new List();

foreach (var item in db.Table())

{

var zad = item.Task.ToString();

data.Add(zad);

}

return data;

}

然后,在MainActivity中,ListView的代碼只添加:

var items = dbr.GetData();

var listView = FindViewById(Resource.Id.listView);

listView.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, items);

我希望將來可以幫助別人.再次感謝大家.

最佳答案 根據(jù)我的評論,您需要進(jìn)行以下更改.

public List GetData()

{

string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "database.db3");

var db = new SQLiteConnection(dbPath);

return db.Table().ToList();

}

然后在您的Activity中,您可以在其中執(zhí)行以下操作

protected override void OnCreate(Bundle bundle)

{

base.OnCreate(bundle);

// Set view

SetContentView(Resource.Layout.Main);

//create database if it doesn't exist

DBRepository dbr = new DBRepository();

dbr.CreateDatabase();

//create table (if it doesn't exist)

dbr.CreateTable();

var items = dbr.GetData();

var listView = FindViewById(Android.Resource.Id.ListView);

listView.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, items);

}

如果您的活動繼承自ListActivity,您可以執(zhí)行以下操作

protected override void OnCreate(Bundle bundle)

{

base.OnCreate(bundle);

// Set view

SetContentView(Resource.Layout.Main);

//create database if it doesn't exist

DBRepository dbr = new DBRepository();

dbr.CreateDatabase();

//create table (if it doesn't exist)

dbr.CreateTable();

var items = dbr.GetData();

ListAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, items);

}

總結(jié)

以上是生活随笔為你收集整理的app把信息添加到mysql_如何将数据库表中的数据添加到ListView C#Xamarin Android App的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。