[英]Database in Xamarin.Android
Hello I have a ListView that shows users
from an SQL-table. 您好,我有一个ListView可以显示来自SQL表的
users
。 I made a drop down menu that appears when I click on the items of the list. 单击列表中的项目后,出现了一个下拉菜单。 My goal is to delete the item I clicked on, but im kinda stuck because I get some errors.
我的目标是删除我单击的项目,但由于遇到一些错误,因此有点卡住了。
Here is my what I am trying to do: 这是我想做的事情:
I created a List<User> users = new List<Users>();
我创建了一个
List<User> users = new List<Users>();
and I my OnCreate
method I have this: 我的
OnCreate
方法中有这个:
lView = FindViewById<ListView>(Resource.Id.usersListView);
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
var db = new SQLiteConnection(dpPath);
users = db.Table<User>().ToList();
adapter = new UsersAdapter(this, users);
lView.Adapter = adapter;
lView.ItemClick += ShowOptions;
Here is my also my code for the delete option from the popup menu 这也是我的弹出菜单中删除选项的代码
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3"); //Call Database
var db = new SQLiteConnection(dpPath);
TableQuery<User> data = db.Table<User>(); //Call Table
** User userToDelete = data.Where(x => x.id == users[e.Position].id).FirstOrDefault(); //Linq Query
if(userToDelete != null)
{
db.Query<User>("DELETE FROM User Where username=?", userToDelete.username);
adapter.NotifyDataSetChanged();
}
When I debug the app I get an Error on the line with **. 当我调试应用程序时,出现错误并带有**。 It says
System.NotSupportedException: Member access failed to compile expression
它说
System.NotSupportedException: Member access failed to compile expression
I would be really thankful if someone tells me what I'm missing or doing wrong. 如果有人告诉我我所缺少或做错的事情,我将非常感激。
UPDATE : 更新 :
User 用户
class User
{
[PrimaryKey, AutoIncrement, Column("_Id")]
public int id { get; set; } // AutoIncrement and set primarykey
[MaxLength(25)]
public string username { get; set; }
[MaxLength(15)]
public string password { get; set; }
public int age { get; set; }
public string email { get; set; }
public override string ToString()
{
return string.Format("Username: {0}, Pass: {1}, Age: {2}, email: {3}", username, password, age, email);
}
}
Adapter 适配器
class UsersAdapter : BaseAdapter<User>
{
List<User> users;
private Context context;
public UsersAdapter(Context cont, List<User> list) : base()
{
users = list;
context = cont;
}
public override long GetItemId(int position)
{
return position;
}
public override int Count
{
get { return users.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override User this[int position]
{
get { return users[position]; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
view = LayoutInflater.From(context).Inflate(Resource.Layout.UserView, null, false);
}
TextView user = view.FindViewById<TextView>(Resource.Id.usernameView);
TextView pass = view.FindViewById<TextView>(Resource.Id.passView);
TextView age = view.FindViewById<TextView>(Resource.Id.ageView);
TextView email = view.FindViewById<TextView>(Resource.Id.emailView);
TextView id = view.FindViewById<TextView>(Resource.Id.idView);
id.Text = string.Format("id #{0}", users[position].id.ToString());
user.Text = users[position].username;
pass.Text = string.Format("Password: {0}", users[position].password);
age.Text = string.Format("Age: {0}", users[position].age);
email.Text = string.Format("Email: {0}", users[position].email);
return view;
}
}
Click 点击
private void ShowOptions(object sender, AdapterView.ItemClickEventArgs e)
{
PopupMenu menu = new PopupMenu(this, lView.GetChildAt(e.Position));
menu.MenuInflater.Inflate(Resource.Menu.popupmenu, menu.Menu);
menu.MenuItemClick += (s, args) =>
{
//different functionality according to the pressed popup menu option
switch (args.Item.ItemId)
{
//delete user from sql table
case Resource.Id.deletePop:
Toast.MakeText(this, string.Format("You clicked Delete"), ToastLength.Short).Show();
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3"); //Call Database
var db = new SQLiteConnection(dpPath);
TableQuery<User> data = db.Table<User>(); //Call Table
//var data1 = data.Where(x => x.id == users[e.Position].id).FirstOrDefault(); //Linq Query
User userToDelete = data.Where(x => x.id == users[e.Position].id).First(); //Linq Query
if(userToDelete != null)
{
db.Query<User>("DELETE FROM User Where username=?", userToDelete.username);
adapter.NotifyDataSetChanged();
}
break;
}
};
menu.Show(); }
I figured it out I had to change TableQuery<User> data = db.Table<User>();
我发现必须更改
TableQuery<User> data = db.Table<User>();
to var data = db.Table<User>().ToList();
到
var data = db.Table<User>().ToList();
. 。 Now it works fine.
现在工作正常。 It seems that the
TableQuery<User>
was not compatible with List<User>
and that's why I got the error. 看来
TableQuery<User>
与List<User>
不兼容,这就是为什么我得到此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.