android - MultiAutoCompleteTextView custom adapter with custom object instead of String Object -
this code working fine want when pass arraylist<string>
adapter. when add object adapter, in multiautocompletetextview
set object full name instead of object full name. want first_name jgetdata
object.
public class myautocomplete extends multiautocompletetextview implements onitemclicklistener { private final string tag = "chipsmultiautocompletetextview"; private handler mhandler = new handler() { public void handlemessage(message paramanonymousmessage) { if (paramanonymousmessage.what == 0) { mhandler.removemessages(0); string str = gettext().tostring().trim(); log.e("handler", "yes"); if (str.length() >= 2) { if(gettext().tostring().contains(",")) // check comman in string { string chips[] = gettext().tostring().trim().split(","); log.e("string", ""+chips[chips.length -1].trim()); makeautosearchtext(chips[chips.length -1].trim()); }else{ makeautosearchtext(str); } } } } }; @suppresslint("newapi") private void makeautosearchtext(string paramstring) { log.e("makeauto", "yes"); string[] ids={"search_name"}; string[] params={paramstring}; getdatatask getdatatask= new getdatatask(ids, params); getdatatask.executeonexecutor(asynctask.thread_pool_executor); } public class getdatatask extends asynctask<void, void, void>{ private string[] ids; private string[] params; private jgetdatalist jgetdatalist; private arraylist<jgetdata> updatedlist; public getdatatask( string[] ids, string[] params) { // todo auto-generated constructor stub this.ids=ids; this.params=params; } @override protected void doinbackground(void... arg0) { // todo auto-generated method stub string response=profiles.getdatafromserver(ids, params, "http://stopnswap.com/service/getalluserlist.php"); jgetdatalist=profiles.parsealluserdata(response); if(jgetdatalist != null){ if(jgetdatalist.success != null){ // adapterlist.clear(); updatedlist= new arraylist<jgetdata>(); for(jgetdata jgetdata : jgetdatalist.success){ updatedlist.add(jgetdata); // adapterlist.add(jgetdata); // log.e("name",jgetdata.name ); } }else{ // adapterlist.clear(); } } log.e("data", ""+response); return null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); if(updatedlist != null){ ((searchlistadapter)getadapter()).setcontactlist(updatedlist); } // adapter.notifydatasetchanged(); } } textwatcher searchviewtextwatch = new textwatcher() { public void aftertextchanged(editable paramanonymouseditable) {} public void beforetextchanged(charsequence paramanonymouscharsequence, int paramanonymousint1, int paramanonymousint2, int paramanonymousint3) {} public void ontextchanged(charsequence paramanonymouscharsequence, int paramanonymousint1, int paramanonymousint2, int paramanonymousint3) { string str=null; if (paramanonymouscharsequence != null) { str = paramanonymouscharsequence.tostring().trim(); if (str.length() < 2) { // break label99; } mhandler.removemessages(0); message localmessage = message.obtain(mhandler, 0, str); mhandler.sendmessagedelayed(localmessage, 400l); } while (str.length() >= 1) { // homesearchformedicinescreen.this.mimagesearch.setvisibility(8); // homesearchformedicinescreen.this.mimagecross.setvisibility(0); return; // label99: // if ((homesearchformedicinescreen.this.msearchview.getadapter() instanceof autocompletesearchadapter)) { // homesearchformedicinescreen.this.mautocompleteadapter.notifydatasetinvalidated(); // } } // homesearchformedicinescreen.this.mimagesearch.setvisibility(0); // homesearchformedicinescreen.this.mimagecross.setvisibility(8); } }; /* constructor */ public myautocomplete(context context) { super(context); init(context); } /* constructor */ public myautocomplete(context context, attributeset attrs) { super(context, attrs); init(context); } /* constructor */ public myautocomplete(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } /* set listeners item click , text change */ public void init(context context){ setonitemclicklistener(this); addtextchangedlistener(searchviewtextwatch); this.setthreshold(1); this.settokenizer(new customcommatokenizer()); } /*textwatcher, if user type country name , press comma following code regenerate chips */ /*private textwatcher textwather = new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { if(count >=1){ if(s.charat(start) == ',') setchips(); // generate chips } } @override public void beforetextchanged(charsequence s, int start, int count,int after) {} @override public void aftertextchanged(editable s) {} }; */ public class customcommatokenizer extends commatokenizer{ @override public charsequence terminatetoken(charsequence text) { charsequence charsequence = super.terminatetoken(text); return charsequence.subsequence(0, charsequence.length()-1); } } /*this function has whole logic chips generate*/ public void setchips(){ if(gettext().tostring().contains(",")) // check comman in string { spannablestringbuilder ssb = new spannablestringbuilder(gettext()); // split string wich comma log.e("auto", gettext().tostring().trim()); string chips[] = gettext().tostring().trim().split(","); int x =0; // loop generate imagespan every country name separated comma for(string c : chips){ // inflate chips_edittext layout layoutinflater lf = (layoutinflater) getcontext().getsystemservice(activity.layout_inflater_service); textview textview = (textview) lf.inflate(r.layout.textview, null); textview.settext(c); // set text // setflags(textview, c); // set flag image // capture bitmapt of genreated textview int spec = measurespec.makemeasurespec(0, measurespec.unspecified); textview.measure(spec, spec); textview.layout(0, 0, textview.getmeasuredwidth(), textview.getmeasuredheight()); bitmap b = bitmap.createbitmap(textview.getwidth(), textview.getheight(),bitmap.config.argb_8888); canvas canvas = new canvas(b); canvas.translate(-textview.getscrollx(), -textview.getscrolly()); textview.draw(canvas); textview.setdrawingcacheenabled(true); bitmap cachebmp = textview.getdrawingcache(); bitmap viewbmp = cachebmp.copy(bitmap.config.argb_8888, true); textview.destroydrawingcache(); // destory drawable // create bitmap drawable imagespan bitmapdrawable bmpdrawable = new bitmapdrawable(viewbmp); bmpdrawable.setbounds(0, 0,bmpdrawable.getintrinsicwidth(),bmpdrawable.getintrinsicheight()); // create , set imagespan ssb.setspan(new imagespan(bmpdrawable),x ,x + c.length() , spannable.span_exclusive_exclusive); setmovementmethod(linkmovementmethod.getinstance()); clickablespan clickspan = new clickablespan() { @override public void onclick(view view) { // deletestring(); // toast.maketext(this, "hi", toast.length_short).show(); log.e("clic", "cl"); deletestring(); } }; ssb.setspan(clickspan,x, x + c.length(),spannable.span_exclusive_exclusive); x = x+ c.length() +1; } // set chips span // settext(ssb); // move cursor last // setselection(gettext().tostring().length()); setspannabletext(ssb); } } public void setspannabletext(final spannable ssb){ new handler().postdelayed(new runnable() { @override public void run() { settext(ssb); setselection(gettext().tostring().length()); } }, 20); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // string mystring= gettext().tostring() +"," +((jgetdata)parent.getitematposition(position)).first_name; // settext(mystring); setchips(); // call generate chips when user select item auto complete // contact contact = (contact) parent.getitematposition(position); // smsutil.selectedcontact.put(contact.num, contact.contactname); // compose.selectedcontact.put(key, value) } private void deletestring(){ int[] startend = getselectionstartandend(); int = startend[0]; int j = startend[1]; //istextdeletedfromtouch = true; // istextadditioninprogress = true; final spannablestringbuilder sb = new spannablestringbuilder(this.gettext() ); string deletedsubstring = sb.subsequence(math.min(i, j), math.max(i, j)).tostring(); // deletefromhashmap(deletedsubstring); boolean hascommaatlast = true; try { sb.subsequence(math.min(i, j + 1), math.max(i, j + 1)) .tostring(); } catch (exception e) { hascommaatlast = false; } sb.replace(math.min(i, hascommaatlast ? j + 1 : j), math.max(i, hascommaatlast ? j + 1 : j), ""); new handler().postdelayed(new runnable() { @override public void run() { settext(sb); new handler().postdelayed(new runnable() { @override public void run() { // todo auto-generated method stub // istextadditioninprogress = false; // stringlength = custommultiautocompletetextview.this.gettext().tostring().length(); // istextdeletedfromtouch = false; //log.i("i replacing text","i replacing text 4"); setmovementmethod(linkmovementmethod.getinstance()); } },50); } }, 10); // setchips(); } private int[] getselectionstartandend(){ int[] startend = new int[2]; startend[0] = this.getselectionstart()<0?0:this.getselectionstart(); startend[1] = this.getselectionend()<0?0:this.getselectionend(); return startend; } }
my adapter
public class searchlistadapter extends arrayadapter<jgetdata>{ private context context; private arraylist<jgetdata> list; public searchlistadapter(context context, arraylist<jgetdata> list) { // todo auto-generated constructor stub super(context, android.r.layout.simple_dropdown_item_1line,list); this.context=context; this.list=list; } @override public int getcount() { // todo auto-generated method stub return list.size(); } @override public jgetdata getitem(int position) { // todo auto-generated method stub return list.get(position); } @suppresswarnings("unchecked") public void setcontactlist(arraylist<jgetdata>list) { // isn't efficient method // need improvise on this.list = list; // this.clonecontactlist = (arraylist<contact>) this.contactlist.clone(); notifydatasetchanged(); } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub holder holder; if (convertview == null) { layoutinflater minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = minflater.inflate(r.layout.item_search_list, null); holder= new holder(); holder.name=(textview)convertview.findviewbyid(r.id.item_search_list_name); convertview.settag(holder); }else{ holder=(holder)convertview.gettag(); } holder.name.settext(""+list.get(position).first_name); // log.e("adapter", ""+list.get(position).first_name); return convertview; } private class holder{ public textview name; } }
the right way achieve implement filterable interface adapter. implement method getfilter() return own implementation of convertresulttostring(object resultvalue) method.
Comments
Post a Comment