SQLite数据库不会更新listview项目而是插入新项目

时间:2015-02-01 17:55:41

标签: android database sqlite listview

点击listview的项目后,它会在另一个edittext的活动中打开。编辑项目后,当我保存它时,项目不会在列表视图中更新,但会在列表视图中插入新条目。

如何在不插入新内容的情况下更新现有项目?

这是我的代码

活动: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);
        }
});

点击listView项后,它会打开另一项活动

TRTimeReminder.class我在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();
}

1 个答案:

答案 0 :(得分:1)

我认为您的适配器存在问题。有两个可能的问题。

  1. 适配器的数据未更新。你说相反,新的价值被添加到ListView。你确定吗?
  2. 更改值后,您尚未调用适配器的方法notifyDataSetChanged()