java - Handle sqlite database in searching -


in app searchbar there, wheren in user types text. whenever text changes in filed call query db related search items. crashes.

here code i'm doing call db

                @override             public boolean onquerytextchange(string newtext) {                 // todo auto-generated method stub                  if(newtext.trim().equals(""))                 {                    return false;                 }                   //showsearchsuggestions(newtext);                 mfilterdata = mcontroller.get_controllerobj().getdbmanager().getallsuggestedfilter(newtext);                  if(msearchadapter != null)                 msearchadapter.swapcursor(mfilterdata);                   return false;             } 

here how m querying in db manager

    public cursor getallsuggestedfilter(string filterstring) {        string read_query = "select * " + tbl_item_table + " "+                              item.title + " like" + "\"%" + filterstring + "%"+"\"";         if(mcursorforfilter != null)        {            mcursorforfilter.close();            mcursorforfilter = null;                }         try        {           mcursorforfilter = getreadabledatabase().rawquery(read_query, null);        }        catch(exception ee)        {         }          return mcursorforfilter; } 

randomly exception

java.lang.illegalstateexception: attempt re-open already-closed object: android.database.sqlite.sqlitequery (msql = select * itemtable title like"%t%") 

that's because closing cursor in wrong place, , trying use after that, cannot use closed cursor.

i rid of part of code:

if(mcursorforfilter != null) {    mcursorforfilter.close();    mcursorforfilter = null;         } 

instead, close old cursor after set new one. swapcursor() returns old cursor, or returns null if there not cursor set, returns null if if try swap same instance of set cursor. knowing that, can try this:

cursor c = msearchadapter.swapcursor(mfilterdata);  if(c != null)     c.close(); 

try that, , let me know if helped.

note when using loader (loadermanager.loadercallbacks), framework going close old cursor. documentation says:

onloadfinished:

the loader release data once knows application no longer using it. example, if data cursor cursorloader, should not call close() on yourself. ...


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -