app把信息添加到mysql_如何将数据库表中的数据添加到ListView C#Xamarin Android App
幾天前我問過如何在活動之間共享數(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnslog在mysql在linux_D
- 下一篇: mysql server安装不成功,解决