我尝试使用新值更新行,但行未更新。我在这里上了这堂课,我试着更新这行:
class EditListItemDialog extends Dialog implements View.OnClickListener {
MyDB dba;
private View editText;
private DiaryAdapter adapter;
private SQLiteDatabase db;
public EditListItemDialog(Context context) {
super(context);
dba = new MyDB(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
dba.open();
}
private List<String> fragment_monday;
private Object position;
private long itemId;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Position is the number of the item clicked
//You can use your adapter to modify the item
long itemId = adapter.getItemId(position); //Will return the clicked item
saveItToDB(itemId);
}
public EditListItemDialog(Context context, DiaryAdapter adapter, int position) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
dba = new MyDB(context);
}
@Override
public void onClick(View v) {
fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
// public void notifyDataSetChanged();
dismiss();
try {
saveItToDB(itemId);
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveItToDB(long itemId) {
dba.open();
dba.updateDiaryEntry(((TextView) editText).getText().toString(), itemId);
dba.close();
((TextView) editText).setText("");
}
}
在saveItToDB dba.updateDiaryEntry(text, itemId);
中应该将行值更改为新值,其中itemId
告诉它必须更新哪一行。它不起作用,我不知道如何解决问题。
任何帮助,将不胜感激。感谢。
编辑---------------------------------------------- ----------------------------------
这是updateDiaryEntry代码:
public boolean updateDiaryEntry(String title, long rowId)
{
ContentValues newValue = new ContentValues();
newValue.put(Constants.TITLE_NAME, title);
return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;
}
我更改了EditListItemDialog类代码,我更新了onItemClick
,saveItToDB
,添加了private long itemId;
并将itemId
添加到了saveItToDB
中的onClick
。你可以验证我的代码是否有任何错误?不幸的是我仍然无法更新sql行。
答案 0 :(得分:0)
替换
return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;
与
return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "= '" + rowId+"'", null)>0;
此外,您还需要确保没有将null
或空String
传递给方法