Updating database from an EditText
<p>I have a text field in SQLite database, which I want the user to be able to update.</p>

<p>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.</p>

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

<p>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).</p>

<p>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.</p>

<pre><code>@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");
}
</code></pre>

<p>}</p>
