SQLite数据库简化版,更新列表视图项,并插入新的,而不是 [英] SQLite Database does't update listview item and inserts new instead
问题描述
在列表视图
项目被点击,它就会在其中具有的EditText
另一个活动开了。编辑项后,当我保存它,项目没有得到在ListView更新,但它插入列表视图中的一个新的条目。
如何更新现有的项目,而不插入新的?
下面是我的code
活动: TRList.class
的ArrayList< HashMap的<字符串,字符串>>项目=新的ArrayList<>();
名单< TRListFormat>表= trDb.getAllReminders();
对于(TRListFormat VAL:名单){
HashMap的<字符串,字符串>图=新的HashMap<>();
map.put(标题,val.getTitle());
map.put(描述,val.getDes());
map.put(日期,val.getDate());
map.put(时间,val.getTime());
items.add(图)
}
适配器=新SimpleAdapter(这一点,项目,R.layout.tr_list_format,
新的String [] {标题,说明,日期,时间},
新的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(适配器);
lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>适配器视图,视图中查看,INT位置,长的id){
意向意图=新的意图(TRList.this,TRTimeReminder.class);
intent.putExtra(remId,(int)的内径);
startActivity(意向);
}
});
在的ListView
项被点击,它会打开另一活动
TRTimeReminder.class
我在菜单中设置保存按钮
案例R.id.menu_tbr_done:
。字符串title = edTitle.getText()的toString();
。字符串DES = edDes.getText()的toString();
字符串时间= timeView.getText()的toString()。
。字符串日期= dateView.getText()的toString();
捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
int值= extras.getInt(remId);
如果(值0){
trDb.updateReminder(新TRListFormat(值,标题,脱,日期,时间));
}
}
其他{
trDb.addReminder(新TRListFormat(标题,脱,日期,时间));
}
意图I =新的意图(getApplicationContext(),TRList.class);
startActivity(ⅰ);
edTitle.getText()清()。
edDes.getText()清()。
dateView.setText(currDate);
timeView.setText(currTime);
返回true;
TRDBHelper.class
//添加新的提醒
无效addReminder(TRListFormat格式){
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues值=新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,空,价值观);
db.close();
}
//更新单提醒
公共无效updateReminder(TRListFormat格式){
SQLiteDatabase DB = this.getWritableDatabase();
INT的id = format.getId();
ContentValues值=新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,价值观,COLUMN_ID +=+ ID,NULL);
db.close();
}
我觉得有问题,您的适配器。有2个可能出现的问题。
- 在适配器的数据不更新。你说的对面,新的价值将被添加到ListView的。你真的确定吗?
- 您还没有叫您的适配器的方法
notifyDataSetChanged()
你已经改变的值之后。
When item of listview
is clicked, it gets opened in another activity which has edittext
. After editing an item, when I save it, item does not get updated in the listview but it inserts a new entry in listview.
How can I update existing item without inserting new?
Here is my code
Activity: 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);
}
});
When listView
item is clicked, it opens another activity
TRTimeReminder.class
I have set save button in menu
case R.id.menu_tbr_done:
String title = edTitle.getText().toString();
String des = edDes.getText().toString();
String time = timeView.getText().toString();
String date = dateView.getText().toString();
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getInt("remId");
if (value > 0) {
trDb.updateReminder(new TRListFormat(value, title, des, date, time));
}
}
else{
trDb.addReminder(new TRListFormat(title, des, date, time));
}
Intent i = new Intent(getApplicationContext(), TRList.class);
startActivity(i);
edTitle.getText().clear();
edDes.getText().clear();
dateView.setText(currDate);
timeView.setText(currTime);
return true;
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();
}
I think there's problem with your adapter. There are 2 possible issues.
- Data of adapter are not updated. You're saying opposite, new value is added to ListView. Are you really sure?
- You haven't called your adapter's method
notifyDataSetChanged()
after you've changed values.
这篇关于SQLite数据库简化版,更新列表视图项,并插入新的,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!