【发布时间】:2017-10-21 13:58:23
【问题描述】:
我正在尝试这样做,以便我可以编辑 SQLite 表中的数据,但它给了我和错误。我想要做的是单击“编辑”按钮,并使用警报对话框中的新字符串编辑已显示在 ListView 中的数据。
更新/编辑代码:
public void UpdateNote(String note)
{
SQLiteDatabase db = this.WritableDatabase;
db.Replace(DB_TABLE, DB_COLUMN + " = ?", new String[] { note });
db.Close();
}
这将从警报对话框正按钮调用。
我收到的错误: enter image description here
更新 1:
自定义适配器:
btnEdit.Click += delegate
{
string noteOld = noteList[position];
mainActivity.UpdateNote(noteOld);
//reload data
mainActivity.LoadNoteList();
};
以上调用 MainActivity:
public void UpdateNote(String noteOld)
{
string oldNote = noteOld;
noteEditText = new EditText(this);
noteEditText.Text = oldNote;
noteEditText.SetLines(5);
noteEditText.SetTextColor(Color.Black);
noteEditText.SetBackgroundColor(Color.White);
noteEditText.SetMaxLines(5);
//noteEditText.SetGravity(Gravity.Top);
alert = new AlertDialog.Builder(this);
alert.SetTitle("Edit Note");
alert.SetMessage("");
alert.SetView(noteEditText);
alert.SetPositiveButton("Save", UpdateOkAction);
alert.SetNegativeButton("Cancel", CancelAction);
alert.Create();
alert.Show();
}
调用MainActivity方法:
public void UpdateOkAction(object sender, DialogClickEventArgs e)
{
string noteNew = noteEditText.Text;
dbHelper.UpdateNote(oldNote);
LoadNoteList();
}
然后转到 DbHelper.cs:
public void UpdateNote(String noteNew)
{
SQLiteDatabase db = this.WritableDatabase;
ContentValues values = new ContentValues();
values.Put("Note", noteNew); // Column name and value respectively
db.Update(DB_TABLE, values, DB_COLUMN + " = ?", new String[] { noteNew });
}
我无法将 noteNew 和 oldNote 都解析到 DbHelper 以替换数据。
【问题讨论】:
标签: c# android mysql sqlite xamarin