Shweta
Shweta

Reputation: 1145

How can I update my EditText values?

I am new to android , as I am trying to update data using context menu item "EDIT". Using EDIT I will go to EditText layout then I will press update Note, But it's not updating the EditText Data.

My Code Database:

public void updateNote(String filename, String filedata, String duedate){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues updatenote = new ContentValues();
    updatenote.put(Col_File_Name, filename);
    updatenote.put(Col_File_Data, filedata);
    updatenote.put(Col_Due_Date, duedate);

    db.update(Table_Notepad, updatenote, Col_ID + "=?", null);
    db.close();
}

Activity:

case R.id.Save:


    String get_title = Title_Edit.getText().toString();
    String get_content = Content_Edit.getText().toString();
    String get_duedate = duedatetext.getText().toString();

    if(!get_title.equals("") || !get_content.equals(""))
    {


     if (!isEdit) {
            //if it isn't edit mode we just add a new note to db

             Database_Notepad db = new Database_Notepad(Add_Task.this);

             db.Create_Note(get_title, get_content, get_duedate);

        } else {
            //if this is edit mode, we just update the old note
             Database_Notepad db = new Database_Notepad(Add_Task.this);

             db.updateNote(get_title, get_content, get_duedate);

            db.close();
        }

    }

Please Suggest, I think I need to correct database code. Please suggest. Thanks

Upvotes: 0

Views: 1347

Answers (3)

RobinHood
RobinHood

Reputation: 10969

Your function updateNote need Record ID which you want to update.

Like

public void updateNote(int id, String filename, String filedata, String duedate){

instead

public void updateNote(String filename, String filedata, String duedate){

and compare that id which your entire record, it will check and update your particular record, change your function function something like below:

public void updateNote(int id, String filename, String filedata, String duedate){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues updatenote = new ContentValues();
    updatenote.put(Col_File_Name, filename);
    updatenote.put(Col_File_Data, filedata);
    updatenote.put(Col_Due_Date, duedate);

    db.update(Table_Notepad, updatenote,  "rowid= "+id,, null);// rowid or name of your id column
    db.close();
}

and pass the id too along with others parameter.

Use

db.updateNote(id, get_title, get_content, get_duedate);

instead

db.updateNote(get_title, get_content, get_duedate);

Upvotes: 1

moDev
moDev

Reputation: 5258

Need to pass row id to update in last argument

db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[]{id});

Happy coding

Upvotes: 0

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12672

In updateNode(), you need to change this update or insert values:

public void updateNote(long rowId, String filename, String filedata, String duedate){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues updatenote = new ContentValues();
    updatenote.put(Col_File_Name, filename);
    updatenote.put(Col_File_Data, filedata);
    updatenote.put(Col_Due_Date, duedate);

    if (rowId >=0)
        db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[] {""+rowId});
    else
        db.insert(Table_Notepad, null, updatenote);
    db.close();
}

Then, if you are creating a new note you can call:

updateNote(-1, filename, filedata, duedate);

and if you are updating an existing one, you will have to pass in the ID:

updateNote(noteRowId, filename, filedata, duedate);.

Source: check these Android docs.

Upvotes: 0

Related Questions