【发布时间】:2016-03-14 19:42:20
【问题描述】:
几天前,我询问如何在活动之间共享数据,一位用户告诉我使用 SQLite,所以我做到了。我想让用户点击 MainLayout 中的按钮,这会将他重定向到 AddTaskLayout,他可以在其中添加任务名称,并通过按保存按钮应用程序将他重定向回 MainLayout,他的任务将在 ListView 中列出。
到目前为止,我创建了数据库、表和我需要的一切。我的问题是:如何将存储在数据库表中的数据添加到 ListView?我找到的每个答案都是用 Java 编写的,所以搜索旧的 StackOverflow 问题并没有太大帮助:/
代码如下:
我的 DBRepository 是代表创建数据库、创建表、向表中插入数据以及获取相同数据的类:
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<ToDoTasks>();
}
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<ToDoTasks>();
foreach(var item in table)
{
output += item;
}
return output;
}
}
我在其中创建表的 ToDoTasks 类:
[Table("ToDo")]
public class ToDoTasks
{
[PrimaryKey, AutoIncrement, Column("_Id")]
public int Id { get; set; }
[MaxLength(100)]
public string Task { get; set; }
}
My AddTaskActivity 代表用户输入任务名称的第二个布局:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.AddTask);
//define buttons
Button save, cancel;
save = FindViewById<Button>(Resource.Id.save);
cancel = FindViewById<Button>(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<EditText>(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<Button>(Resource.Id.addTask);
addTask.Click += addTask_click;
}
private void addTask_click(object sender, EventArgs e)
{
StartActivity(typeof(AddTaskActivity));
}
非常感谢您的帮助。我知道这些是非常基本的问题,但必须有人为自己和许多其他(未来)C# android 开发人员提出这些问题。谢谢!
///////////////////
更新:我检查了 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<string> GetData()
{
string dbPath = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
List<string> data = new List<string>();
foreach (var item in db.Table<ToDoTasks>())
{
var zad = item.Task.ToString();
data.Add(zad);
}
return data;
}
然后,在 MainActivity 中 ListView 的代码在哪里只添加:
var items = dbr.GetData();
var listView = FindViewById<ListView>(Resource.Id.listView);
listView.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
我希望它会在未来对其他人有所帮助。再次感谢各位。
【问题讨论】:
-
Xamarin 有多个示例演示如何执行此操作 - 查看 Tasky:github.com/xamarin/mobile-samples/tree/master/Tasky/…
-
@Jason:Tasky 不是很有帮助,对于像我这样的初学者来说太复杂的代码:/
-
首先,您需要更改
GetData()方法以返回ToDoTasks列表(即IEnumerable<ToDoTasks>)。完成后,您将需要使用适配器(我认为SimpleAdapter对于您的初始版本应该可以正常工作,可能希望在某个时候创建自己的适配器)。然后你需要将适配器设置为ListView。 -
我的意思是“将适配器设置为 ListView”,我的意思是您需要在 ListView 上设置适配器(即不完全确定语法,但它可以是
listView.SetAdapter(adapter)或 @987654335 @) -
@Johan:你能告诉我如何使用 IEnumerable,好吗?编辑:所以我需要将公共字符串 GetData() 更改为公共 IEnumerable
GetData() 并返回字符串输出?
标签: c# android sqlite android-listview xamarin