【发布时间】:2017-08-10 19:27:44
【问题描述】:
我正在使用此代码创建新的 sql 数据库并存储我的项目。
我已经用这段代码创建了一个类:
class Contact
{
public string Name { get; set; }
public Contact(string name)
{
Name = name;
}
public Contact()
{
}
然后在我的主要活动中
string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "dbTest.db3");
private List<string> mitems;
private ListView mlistview;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button InsertAndShow = FindViewById<Button>(Resource.Id.button2);
InsertAndShow.Click += delegate
{
EditText text = FindViewById<EditText>(Resource.Id.editext1);
var db = new SQLiteConnection(dbPath);
Contact mycontact = new Contact(text.Text);
db.Insert(mycontact);
var table = db.Table<Contact>();
foreach (var item in table)
{
mlistview = FindViewById<ListView>(Resource.Id.listView1);
mitems = new List<string>();
mitems.Add(Convert.ToString(item.Name));
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mitems);
mlistview.Adapter = adapter;
}
};
但它并没有显示我的所有物品。它只显示最后一项。
【问题讨论】:
标签: c# android sqlite xamarin xamarin.android