android - removing, restoring items in ListView and database -


i have listview items added dynamically. when item added, added listview , inserted sqlite database items in list can saved. can remove specific items getting postion if item , removing information.

this how add items (i use multiple classes):

in datamodel.class:

public void additem(object data) {          string string = string.valueof(data);          mplanetslist.add(createplanet("planet", string));          history history = new history();         history.getdatahashmap().put("planet", data);         history.addtohistorydb();          mhistorylist.add(history);          if (null != monitemaddhandler) {             monitemaddhandler.onitemadded(data);         }     } 

in history.class:

public void addtohistorydb() {          mysqliteopenhelper mdbhelper = mysqliteopenhelper.getinstance();         sqlitedatabase db = mdbhelper.getwritabledatabase();          contentvalues values = getcontentvalues();          rawid = (int) db.insert(table_name, col_id, values);         db.close();     } 

this how remove items:

in datamodel.class:

public void removeitem(int index) {           if (mplanetslist == null || index < 0 || index >= mplanetslist.size()) {             return;         }         // remove mplanetlist         mplanetslist.remove(index);          history history = mhistorylist.get(index);         mhistorylist.remove(index);         if (null != history) {             history.remove();             history = null;         }          // notify, historyfragment update view         if (null != monitemaddhandler) {             monitemaddhandler.onitemremove(index);         } 

in history.class:

public void remove() {         mysqliteopenhelper mdbhelper = mysqliteopenhelper.getinstance();         sqlitedatabase db = mdbhelper.getwritabledatabase();          int r = db.delete(table_name, "_id = ?",  // delete int r ??                 new string[] { string.valueof(rawid) });          db.close();     } 

when app in removed 'recent apps' or restarted, initlist() called restored listview items database.

this code:

datamodel:

private void initlist() {            mhistorylist = history.getlist();         (int = 0; < mhistorylist.size(); i++) {             object obj = mhistorylist.get(i).getdatahashmap().get("planet");             mplanetslist.add(createplanet("planet", string.valueof(obj)));          }     } 

history:

public static arraylist<history> getlist() {         cursor cursor = null;         try {             mysqliteopenhelper mdbhelper = mysqliteopenhelper.getinstance();             sqlitedatabase db = mdbhelper.getwritabledatabase();             string sql = "select * " + history.table_name;             arraylist<history> list = new arraylist<history>();             cursor = db.rawquery(sql, null);             if (cursor != null) {                 (cursor.movetofirst(); !cursor.isafterlast(); cursor                         .movetonext()) {                     history item = new history();                     item.fromcuror(cursor);                     list.add(item);                 }             }             return list;         } {             if (null != cursor && integer.parseint(build.version.sdk) < 14) {                 cursor.close();             }         }     } 

this method works fine long order of list not changed - can add items , removes items , works perfectly. however, items added top of list rather bottom.

so read around on how add items top of listview , found adding 0 position when adding item, new items go top of list.

mplanetslist.add(0, createplanet("planet", string)); // put 0 in wherever item added eg when adding database 

when this, however, items viewed/deleted in listview not continue correspond data in database. example, lets add 3 items list: dog cat horse. listview shows them in correct order (horse @ top). if delete item, lets say: cat , listview updates , everyting good. if restart app, initlist() called. problem occurs - not cat removed list, horse or dog.

possible points think problem be:

a) in initlist() - not adding correct items

b) in removeitem() - not removing correct items database.

c) in getlist() - maybe cursor???

this think b) more don't know problem or change too.

edit:

after little testing, found right b) goes wrong.

the steps reproduce:

create 3 items: cat dog horse. listview shows them in correct order ie horse @ top, cat @ bottom. database shows them cat @ top, horse @ bottom.

when delete horse (top of listview, bottom of database) listview updates correctly, in database not horse has been deleted in fact cat.

so because of wrong removal, when database used restore listview, listview appears wrong.

i hope makes things clearer!

edit 2: how create table:

public static final string table_name = "history_table";     public static final string col_title = "title";     public static final string col_content = "content";     public static final string col_date = "date";     public static final string col_type = "type";     public static final string col_ongoing = "ongoing";     public static final string col_data = "data";     public static final string col_id = "_id";      public static final string database_create = "create table " + table_name             + "(" + col_id + " integer primary key autoincrement, " + col_title             + " text not null, " + col_content + " text not null, " + col_date             + " text not null, " + col_type + " text not null, " + col_ongoing             + " text not null, " + col_data + " text not null  );"; 

this getcontentvalues():

public contentvalues getcontentvalues() {          contentvalues values = new contentvalues();         values.put(history.col_title, "mytitle");         values.put(history.col_content, "mycontent");         values.put(history.col_date, "date placeholder");         values.put(history.col_type, "quicknote");         values.put(history.col_ongoing, "ongoing placeholder");         values.put(history.col_data, new jsonobject(mdatahashmap).tostring());          return values;     } 

edit 3:

i have narrowed down fact because order of items in listview , in database different, deleting items postition in list not work. possible solution invert order of items in database match listview (good idea / possible ?).

edit 3.1:

https://stackoverflow.com/a/7348117/2442638

this order descending may help. don't know should put though

edit 3.5:

i have ordering working. deleting works, there problem first deletion doesn't work. rest work after that. sorting _id , autoincrement maybe starts @ 1 or 0 , list starts @ other. more research

there 2 list in app: mhistorylist , mplanetslist, should keep order of data itmes in them same.

you can add items top of listview calling method additem in datamodel:

public void additem(object data) {      history history = new history();     history.getdatahashmap().put("planet", data);     history.addtohistorydb();      mhistorylist.add(0, history);     mplanetslist.add(0, history.createplanet());      if (null != monitemaddhandler) {         monitemaddhandler.onitemadded(data);     } } 

then new data @ top of listview.

after app restart, code below called:

private void initlist() {      mhistorylist = history.getlist();     (int = 0; < mhistorylist.size(); i++) {         mplanetslist.add(mhistorylist.get(i).createplanet());     } } 

we should make sure data history.getlist(); has same order, in word, new data time in head of list.

so should change code (order _id desc):

public static arraylist<history> getlist() {     cursor cursor = null;     try {         mysqliteopenhelper mdbhelper = mysqliteopenhelper.getinstance();         sqlitedatabase db = mdbhelper.getwritabledatabase();          // order _id desc         string sql = "select * " + history.table_name + " order _id desc";         arraylist<history> list = new arraylist<history>();         cursor = db.rawquery(sql, null);         if (cursor != null) {             (cursor.movetofirst(); !cursor.isafterlast(); cursor.movetonext()) {                 history item = new history();                 item.fromcuror(cursor);                 list.add(item);             }         }         return list;     } {         if (null != cursor && integer.parseint(build.version.sdk) < 14) {             cursor.close();         }     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -