-1

I display a listview with database rows. When a row is clicked, a dialog pops up with the (editable) items of that row.

public class ViewDataActivity extends ListActivity {

private ListView listView;

HashMap<Integer, String> results = new HashMap<Integer, String>();;
ArrayList<String> dataValues = new ArrayList<String>();
ArrayList<Integer> keyValues = new ArrayList<Integer>();

ArrayAdapter<String> arrayAdapter;

Integer itemKey;
String itemValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    results = (HashMap<Integer, String>) getIntent().getSerializableExtra(MainActivity.EXTRA_MESSAGE);

    for(Map.Entry<Integer, String> entry: results.entrySet()) {
        keyValues.add(entry.getKey());
        dataValues.add(entry.getValue());
    }

    // Display items
    listView = getListView();
    View v = getLayoutInflater().inflate(R.layout.list_view_header, null);
    listView.addHeaderView(v);

    arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            dataValues );

    listView.setAdapter(arrayAdapter);

    // Make Items clickable
    listView.setChoiceMode(CHOICE_MODE_SINGLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.d("List position clicked", String.valueOf(listView.getCheckedItemPosition()));
            itemKey = keyValues.get(listView.getCheckedItemPosition()-1);
            itemValue = dataValues.get(listView.getCheckedItemPosition()-1);
            showInputBox(itemKey, itemValue);
        }
    });

}

// Show dialog with values
public void showInputBox(final Integer itemKey, String listItemValue){
    final Dialog dialog=new Dialog(ViewDataActivity.this);
    dialog.setTitle("Edit Item");
    dialog.setContentView(R.layout.list_item_edit_popup);
    TextView txtMessage=(TextView)dialog.findViewById(R.id.txtmessage);
    txtMessage.setText("Update");
    txtMessage.setTextColor(Color.parseColor("#ff2222"));

    String[] listItemValues = listItemValue.split(",");

    final String liCategory = listItemValues[2];
    final String liDescription = listItemValues[3];
    final String liAmount = listItemValues[4];

    EditText editText1=(EditText)dialog.findViewById(R.id.txtinput1);
    EditText editText2=(EditText)dialog.findViewById(R.id.txtinput2);
    EditText editText3=(EditText)dialog.findViewById(R.id.txtinput3);

    editText1.setText(liCategory);
    editText2.setText(liDescription);
    editText3.setText(liAmount);

    Button bt=(Button)dialog.findViewById(R.id.btdone);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Write new Values
            dbHelper.updateExpense(itemKey, liCategory, liDescription, liAmount);
            // Refresh ListView
            arrayAdapter.notifyDataSetChanged();
            dialog.dismiss();
        }
    });
    dialog.show();
}

}

But when i update the values in the dialog and click the button, the dialog closes but the row isn't updated in the database. The update being executed is this:

public boolean updateExpense(Integer id, String category, String description, String amount) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_CATEGORY, category);
    contentValues.put(COLUMN_DESCRIPTION, description);
    contentValues.put(COLUMN_AMOUNT, amount);
// Returns 1 but no update    
Log.d("DBUpdate", String.valueOf(db.update(TABLE_NAME, contentValues, COLUMN_ID + " = ? ", new String[] { Integer.toString(id) } )));
    return true;
}

What am i missing?

Jay
  • 1,392
  • 7
  • 17
  • 44
  • Why not using [CursorLoader](https://developer.android.com/reference/android/content/CursorLoader.html) to update ListView without requery database again? – ρяσѕρєя K Nov 03 '16 at 12:52
  • *What am i missing?* the code where you are checking that there was *no update* – Selvin Nov 03 '16 at 12:53
  • @Selvin Can you elaborate? The same code that was used to populate the listview can be used to verify whether or not a row has been updated. – Jay Nov 04 '16 at 06:53
  • Any reason as to why the question was downvoted? – Jay Nov 04 '16 at 06:54

1 Answers1

0

Hard to tell as clearly the code you're showing us is incomplete, but if you're using database.beginTransaction();, make sure you use database.setTransactionSuccessful(); after your update call. Also don't forget database.endTransaction(); in your finally statement.

vkislicins
  • 3,331
  • 3
  • 32
  • 62
  • That IS the entire code showing the relevant dialog and the db update operation. Let me know if you would like any specific information – Jay Nov 04 '16 at 06:55
  • Ok, then maybe try wrapping your call in the above methods. Might help.. – vkislicins Nov 04 '16 at 12:04
  • Also when you're saying the database was not updated - how do you know that? Did you pull the db from the phone and checked? – vkislicins Nov 04 '16 at 12:06
  • yes i reloaded the app and listview and noticed that the row was sill the same. I'll try the methods you mentioned – Jay Nov 04 '16 at 19:27
  • vkislins, Unfortunately still not updating :( – Jay Nov 04 '16 at 20:23
  • @Jay try grabbing the database from the phone and checking the field is updated in `sqlitebrowser`. You can grab your db by going to your terminal, then typing `adb shell`, then `run-as `, then `cd ./databases`, just copy the db to local storage using `cp ./.db /sdcard/` then `exit` and `exit` and `adb pull /sdcard/.db ` to get it to your machine, Then open with `sqlitebrowser`, and have a look if the row you expected to update was updated. Maybe there's an error in how you get your row for the listview. – vkislicins Nov 05 '16 at 11:12