retrieving data from Sqlite database is getting slower at afterTextChanged method in android -


i have edittext box.in text box used aftertextchanged(editable s) method. have database containing 60,000 data. making dictionary. aim when user enter text textbox(edittext),it shows 10 rows listview database matching entered text.

in mainactivity used aftertextchanged(editable s)

        @override         public void aftertextchanged(editable s) {             // todo auto-generated method stub              mdbhelper.open();              string ss=s.tostring();               arraylist<hashmap<string, string>> list_of_wordmeanings = new arraylist<hashmap<string, string>>();              cursor mcursor = mdbhelper.gettestdata(ss); // here got result database              (int = 0; < mcursor.getcount(); i++)              {                 mcursor.movetoposition(i);                                                        hashmap<string, string> hm = new hashmap<string, string>();                      string word = mcursor.getstring(0).tostring();                     string meaning = mcursor.getstring(1).tostring();                        hm.put("key_word",word);                       hm.put("key_meaning",meaning);                        list_of_wordmeanings.add(hm);             }                      string[] = { "key_word","key_meaning" };                      int[] = { r.id.txt1,r.id.txt2};                      simpleadapter adapter = new simpleadapter(getbasecontext(), list_of_wordmeanings, r.layout.list_layout, from, to);                      listview.setadapter(adapter);                      mdbhelper.close();          }     }); 

my used function data database that:

public cursor gettestdata(string s)  {      try      {       //string sql ="select * table1 limit 5";//"select  * " + table_registration +" "+ key_name +" '"+ s +"%'" ;           string sql ="select  * dic english '"+ s +"%' limit 5" ;           cursor mcur = mdb.rawquery(sql, null);          if (mcur!=null)          {             mcur.movetonext();          }          return mcur;      }      catch (sqlexception msqlexception)       {          log.e(tag, "gettestdata >>"+ msqlexception.tostring());          throw msqlexception;      }  } 

everything working problem showing matching result slow. how can in faster way?? can give me proper solution????

your aftertextchanged() method inefficient. improve following changes:

don't open , close database each time user changes text(and aftertextchanged() method get's called). don't mdbhelper.open(); every time, instead:

if (!mdbhelper.isdatabaseopen()) {     // database not open open     mdbhelper.open();// future text entered database open   } 

you'll close database @ later point(like onpause of activity).

there's no reason extract data cursor , put in hashmap, cursor more efficient , not mention extraction create hundreds of objects should avoid. if you're doing use simpleadapter, use cursor along simplecursoradapter(or make own adapter).

don't make new adapter each time instead reuse first instance , update data of adapter(followed notifydatasetchanged() call).

you shouldn't make database queries on main ui thread(like in aftertextchanged() mdbhelper.gettestdata(ss) call). also, based on text entered user require 1 database query. example if user enters a cursor rows query. if enters ab it's safe assume filtered rows in initial set(the cursor returned a) @ cursor data , pick values need. have @ cursorwrapper this.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -