android - Listview filter the data contained in database very slowly -
i'm working english persian dictionary ... unfortunately, not using lazzy listview sample in project , project had list , filter
out 50 thousand words database , slow typing , updating list occured , please me use lazzy listview
in project , use optimized filter reduce use of memory , increase speed .... tnx
this classes :
mainactivity
public class mainactivity extends activity { listview list; imageview cleartext; edittext editsearch; typeface edtsearchfont; static string[] word; static int[] index; static listviewadapter adapter; static arraylist<listviewstruct> arraystructlist = new arraylist<listviewstruct>(); context context; // oncreate @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listview_main); context = this; cursor en = g.database.rawquery("select * english", null); word = new string[en.getcount()]; index = new int[en.getcount()]; int = 0; listviewstruct wp = null; if (en.movetofirst()) { { try { word[i] = en.getstring(en.getcolumnindex("english_word")); index[i] = en.getint(en.getcolumnindex("word_id")); wp = new listviewstruct(word[i], index[i]); arraystructlist.add(wp); i++; } catch (exception e) { e.printstacktrace(); } } while (en.movetonext()); } en.close(); list = (listview) findviewbyid(r.id.listview); adapter = new listviewadapter(this, arraystructlist); list.setadapter(adapter); editsearch = (edittext) findviewbyid(r.id.search); edtsearchfont = typeface.createfromasset(context.getassets(), "fonts/listfont.ttf"); editsearch.settypeface(edtsearchfont); editsearch.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) { string text = editsearch.gettext().tostring().tolowercase(locale.getdefault()); adapter.filter(text); } }); cleartext = (imageview) findviewbyid(r.id.cleartextimage); cleartext.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { editsearch.settext(""); } }); } @override public boolean oncreateoptionsmenu(menu menu) { //getmenuinflater().inflate(r.menu.activity_main, menu); menu.add(0, 1, 1, "about"); menu.add(0, 2, 2, "exit"); return true; } //@suppresswarnings("deprecation") @override public boolean onoptionsitemselected(menuitem item) { // todo auto-generated method stub switch (item.getitemid()) { case 1: { alertdialog.builder builder = new alertdialog.builder(mainactivity.this); builder.settitle("about") .setmessage("\n" + "© copyright" + "\n" + "hossein tahmasebi , 2014" + "\n") .setcancelable(false) .seticon(r.drawable.logo) .setnegativebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { dialog.cancel(); } }); alertdialog alert = builder.create(); alert.show(); break; } case 2: { this.finish(); } } return super.onoptionsitemselected(item); };
}
listviewadapter
public class listviewadapter extends baseadapter { // declare variables context mcontext; layoutinflater inflater; private list<listviewstruct> localwordslist; private arraylist<listviewstruct> arraystructlist; public int index; typeface listfont; public listviewadapter(context context, list<listviewstruct> mainwordlist) { mcontext = context; this.localwordslist = mainwordlist; inflater = layoutinflater.from(mcontext); this.arraystructlist = new arraylist<listviewstruct>(); this.arraystructlist.addall(mainwordlist); } public class viewholder { textview word; } @override public int getcount() { return localwordslist.size(); } @override public listviewstruct getitem(int position) { return localwordslist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(final int position, view view, viewgroup parent) { final viewholder holder; listfont = typeface.createfromasset(mcontext.getassets(), "fonts/listfont.ttf"); if (view == null) { holder = new viewholder(); view = inflater.inflate(r.layout.listview_item, null); holder.word = (textview) view.findviewbyid(r.id.word); view.settag(holder); } else { holder = (viewholder) view.gettag(); } holder.word.settext(localwordslist.get(position).getword()); holder.word.settypeface(listfont); view.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { intent intent = new intent(mcontext, entranslatedresultactivity.class); intent.putextra("index", (localwordslist.get(position).getindex())); intent.putextra("word", (localwordslist.get(position).getword())); mcontext.startactivity(intent); } }); return view; } // filter class public void filter(string chartext) { chartext = chartext.tolowercase(locale.getdefault()); localwordslist.clear(); if (chartext.length() == 0) { localwordslist.addall(arraystructlist); } else { (listviewstruct wp: arraystructlist) { if (wp.getword().tolowercase(locale.getdefault()).startswith(chartext)) { localwordslist.add(wp); } } } notifydatasetchanged(); }
}
listviewstruct
public class listviewstruct { private int index; private string word; private string translatedword; public listviewstruct(string word, int index) { this.word = word; this.index = index; } public int getindex() { return this.index; } public string getword() { return this.word; } public string getpopulation() { return this.translatedword; } }
g class
, helperio
, entranslatedresultactivity
show translated result word.....
if these classes required here , tell me put them ,
thank attention.....
this example of cursoradapter(use instead of baseadapter) need pass cursor , don't need put cursor in arraylist in while loop
public class imagecursoradapter extends cursoradapter { public imagecursoradapter(context context, cursor c) { super(context, c); } @override public void bindview(view view, context context, cursor cursor) { textview word = (textview) view.findviewbyid(r.id.word); word.settext(cursor.getstring(cursor.getcolumnindex("english_word"))); word.settypeface(listfont); view.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { intent intent = new intent(context, entranslatedresultactivity.class); intent.putextra("index", (cursor.getint(cursor.getcolumnindex("word_id"))); intent.putextra("word", (cursor.getcolumnindex("english_word"))); context.startactivity(intent); } }); } @override public view newview(context context, cursor cursor, viewgroup parent) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); view convertview = infalinflater.inflate(r.layout.listview_item, null); bindview(convertview , context, cursor); return convertview ; } }
in newview getview method (without viewholder)
, in bindview need set data view can set edittext , bind adapter set ontextchangelistener edittext have fast search when user typing in edittext.
better way better performance exist share know
Comments
Post a Comment