SQLite database does not update list item and inserts new one instead

When the listview element is clicked, it opens in another action with edittext . After editing an item, when I save it, the item is not updated in the list, but it inserts a new entry into the list.

How to update an existing item without inserting a new one?

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); } }); 

Clicking the listview button opens another action

TRTimeReminder.class I set the save button to 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(); } 
-2
source share
1 answer

I think the problem is with your adapter. There are two possible problems.

+1
source

Source: https://habr.com/ru/post/1496721/


All Articles