java - Can't update item in database [sqlite] -


can't find error in code. i'm sure, there might problems id's. maybe queries wrong. kinda new stuff.

mydbhelper

public void onclick(dialoginterface dialog, int whichbutton) {                 string edittextvalue = edittext.gettext().tostring();                 task t = new task(edittextvalue);                 dbhandler.updatetask(t);                   if (dbhandler.updatetask(t) == true){                     toast.maketext(mainactivity.this, "ir", toast.length_long).show();                 }                 else                 {                     toast.maketext(mainactivity.this, "naah", toast.length_long).show();                 }                   displaytasklist();              } 

update function:

 public boolean updatetask(task t){     boolean result = false;     string q = "select * " + table_name + " " + column_id + " = " + t.getid();     sqlitedatabase db = this.getwritabledatabase();     cursor c = db.rawquery(q, null);     if (c.movetofirst())     {         string q2 = "update " + table_name + " set " + column_task + " = "                 + t.gettask() + " " + column_id + " = " + t.getid();         db.execsql(q2);         result = true;     }     db.close();     return result; } 

task class:

public class task { private int _id; private string _task;  public task() {} public task(string task){     this._task = task; } public task (int id, string task){     this._id = id;     this._task = task; }  // sets&gets public void setid(int id) {     this._id = id; } public int getid() {     return this._id; } public void settask(string task){     this._task = task; } public string gettask(){     return this._task; } } 

mainactivity:

// detect list item selected (by id) private long _id; public void setid(long id) { this._id = id; } public long getid() { return this._id; }  // attaches long click listener listview private void setuplistviewlistener() {     lvtasks.setonitemlongclicklistener(             new adapterview.onitemlongclicklistener() {                 @override                 public boolean onitemlongclick(adapterview<?> adapter,                                                view item, int pos, long id) {                     item1.setvisible(true); item2.setvisible(true); item3.setvisible(true); // showing buttons                     setid(id); //catches selected items id                     return true;                 }              }); } 

the problem might mixes id's? instance in mainactivity id selected task (onlongclick), in task/mydbhelper class id newly created task - though they're not connected(?). great.

you can use update function this:

 public int updatetask(task t){  //i assume open db here  sqlitedatabase db = this.getwritabledatabase();   //put values needs updated in values column names ,  values  contentvalues values = new contentvalues();  values.put(column_task,t.gettask());  values.put(column_id,t.getid());   // db.update(string table,contentvalues values,string whereclause,string[] wherearguments)  int result = db.update(table_name,values,column_id+"=?"+new string[] {string.valueof(t.getid())});  db.close();  return result;  } 

i guess solves problem.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -