tags:

views:

22

answers:

1

Hi,

I have a text field in SQLite database, which I want the user to be able to update.

So I extract and display the text in an EditText view. The user edits it and presses "Update" button. At this point I want to update the text field in database with whatever user has in the EditText.

Here's the problem, EditText returns the type Editable.
And the put(String key, String value) function of ContentValues, that I use while updating the record, expects a String. So how to put an Editable in a String?

I tried upcasting the Editable to CharSequence and then casting that to String, but that gives me a runtime exception, ClassCastException, which it very well should (because the object was an Editable to begin with, I cannot make it a String).

So how to get around this? Here's the code I am using. I know it is wrong, but I cannot figure out how to do this.

@Override
public void onClick(View v) {
   // update logic
   CharSequence strNewName = editTextName.getText();

  //Update record in the table
  ContentValues newRecord = new ContentValues();
  newRecord.put(GIDatabase.Students.NAME, (String strNewName); //ClassCastException here
  int rowsEffected = db.update(GIDatabase.Students.STUDENTS_TABLE_NAME, newRecord, GIDatabase.Students._ID + "=?", new String[]{strId});
if (rowsEffected != 1) {
    // This should never be reached                         
         throw new RuntimeException("Error updating name");
}

}

+1  A: 

This should do it:

String.valueOf(editTextName.getText())
Cpt.Ohlund
Thanks buddy. I later thought of toString() function, but your answer is better (in case of nulls etc).
OceanBlue
toString() is the standard way of doing it that I've seen
Falmarri