Android SQLite数据库没有';t更新listview项并插入新项
单击Android SQLite数据库没有';t更新listview项并插入新项,android,database,sqlite,listview,Android,Database,Sqlite,Listview,单击listview的项目时,它将在另一个具有edittext的活动中打开。编辑项目后,当我保存它时,该项目不会在listview中更新,但会在listview中插入一个新条目 如何更新现有项目而不插入新项目? 这是我的密码 活动:TRList.class ArrayList<HashMap<String, String>> items = new ArrayList<>(); List<TRListFormat> list = trDb.getA
listview
的项目时,它将在另一个具有edittext
的活动中打开。编辑项目后,当我保存它时,该项目不会在listview中更新,但会在listview中插入一个新条目
如何更新现有项目而不插入新项目?
这是我的密码
活动:TRList.class
ArrayList<HashMap<String, String>> items = new ArrayList<>();
List<TRListFormat> list = trDb.getAllReminders();
for (TRListFormat val : list) {
HashMap<String, String> map = new HashMap<>();
map.put("title",val.getTitle());
map.put("description", val.getDes());
map.put("date", val.getDate());
map.put("time", val.getTime());
items.add(map);
}
adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
new String[] { "title", "description", "date", "time" },
new int[] {R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time });
lv = (ListView)findViewById(R.id.tbr_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(TRList.this, TRTimeReminder.class);
intent.putExtra("remId", (int)id);
startActivity(intent);
}
});
TRDBHelper.class
//Add new reminder
void addReminder(TRListFormat format){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, format.getTitle());
values.put(COLUMN_DES, format.getDes());
values.put(COLUMN_DATE, format.getDate());
values.put(COLUMN_TIME, format.getTime());
db.insert(TABLE_NAME, null, values);
db.close();
}
//Update single reminder
public void updateReminder(TRListFormat format){
SQLiteDatabase db = this.getWritableDatabase();
int id = format.getId();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, format.getId());
values.put(COLUMN_TITLE, format.getTitle());
values.put(COLUMN_DES, format.getDes());
values.put(COLUMN_DATE, format.getDate());
values.put(COLUMN_TIME, format.getTime());
db.update(TABLE_NAME, values, COLUMN_ID+ " = " +id, null);
db.close();
}
我想你的适配器有问题。有两个可能的问题
请在创建适配器的地方发布代码。我不确定是否使用SQLite,但我在不久前编写的应用程序中使用MS SQL Server观察到了这种行为。如果没有要更新的行,则更新调用插入了一行。是否确实调用了
updateReminder
方法,而不是addReminder
?@Marcus如果您看到“我的保存”按钮的代码,我已将updateReminder
设置为id>0,否则它将插入新的提醒。请看“如果…”或“其他…”。。在我的代码中。我想我在那里做错了什么。@Rohit5k2谢谢,但我想这与适配器无关。它插入一个项目并检索,所以我的适配器不会出错。您能告诉我如何更新适配器吗?是的,它确实在listview中创建了一个新条目。我在TRList的开头添加了适配器代码。class
检查我的问题。解决了它!我需要对TRList.class的意图做一点更改,我刚刚更改了intent.putExtra(“remId”,(int)id)代码>带有intremid=(int)id+1;Bundle-dataBundle=新Bundle();dataBundle.putInt(“remId”,remId);意向。额外支出(数据绑定)代码>
//Add new reminder
void addReminder(TRListFormat format){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, format.getTitle());
values.put(COLUMN_DES, format.getDes());
values.put(COLUMN_DATE, format.getDate());
values.put(COLUMN_TIME, format.getTime());
db.insert(TABLE_NAME, null, values);
db.close();
}
//Update single reminder
public void updateReminder(TRListFormat format){
SQLiteDatabase db = this.getWritableDatabase();
int id = format.getId();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, format.getId());
values.put(COLUMN_TITLE, format.getTitle());
values.put(COLUMN_DES, format.getDes());
values.put(COLUMN_DATE, format.getDate());
values.put(COLUMN_TIME, format.getTime());
db.update(TABLE_NAME, values, COLUMN_ID+ " = " +id, null);
db.close();
}