-2

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();
}
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • Please post the code where you are creating your adapter. – Rohit5k2 Feb 01 '15 at 18:18
  • I'm not sure about SQLite, but I observed this behavior using MS SQL Server for an application I wrote a while back. The call to Update inserted a row if there was no row to be updated. – Marcus Feb 01 '15 at 18:18
  • Are you sure that your `updateReminder` method is called, and not `addReminder`? – Marcus Feb 01 '15 at 18:24
  • @Marcus if you see the code of my **save button**, I have set `updateReminder` only when the id is >0, otherwise it will insert a new reminder. See the if..else.. in my code. I guess I'm doing something wrong there. – Apurva Feb 01 '15 at 18:33
  • @Rohit5k2 Thanks but I guess it has nothing to do with adapter. It inserts an item and retrieves too so there is no mistake my adapter. – Apurva Feb 01 '15 at 18:35
  • http://stackoverflow.com/questions/10978136/sqlite-db-update, http://stackoverflow.com/questions/9798473/sqlite-in-android-how-to-update-a-specific-row – RajaReddy PolamReddy Feb 03 '15 at 04:06
  • Sorry but i don't think `db.update` notifies your adapter, if i'm not wrong to do that you need to use a `ContentResolver` – MineConsulting SRL Feb 03 '15 at 10:01

1 Answers1

1

I think there's problem with your adapter. There are 2 possible issues.

  1. Data of adapter are not updated. You're saying opposite, new value is added to ListView. Are you really sure?
  2. You haven't called your adapter's method notifyDataSetChanged() after you've changed values.
skywall
  • 3,956
  • 1
  • 34
  • 52
  • could you tell me how to update adapter? And yes it does make a new entry in listview. – Apurva Feb 02 '15 at 15:07
  • I have added adapter code at the beginning of `TRList.class` check my question. – Apurva Feb 03 '15 at 09:50
  • Solved it! I needed to make a little change in intent of `TRList.class` I just changed `intent.putExtra("remId", (int)id);` with `int remId = (int)id + 1; Bundle dataBundle = new Bundle(); dataBundle.putInt("remId", remId); intent.putExtras(dataBundle);` – Apurva Feb 03 '15 at 19:15