android - AsyncTask doesn`t work in custom adapter -
i`m create list of points of sales sqlite using custom adapter
dbhelper = new yourvoicedatabasehelper(getactivity()); pointslist = new arraylist<point>(); list<point> pl = new arraylist<point>(); pointslistarrayadapter adapter; adapter = new pointslistarrayadapter(getactivity().getapplicationcontext(), pointslist, getactivity().getfilesdir(), getresources().getstring(r.string.local_resorces_folder), dbhelper); pointslistview.setadapter(adapter); pl = dbhelper.getallpoints(); if(mylocation != null) { (point el : pl) { ellocation.setlatitude(double.parsedouble(el.getlatitude())); ellocation.setlongitude(double.parsedouble(el.getlongtitude())); if (mylocation.distanceto(ellocation) <= 999) { pointslist.add(el); } } } else { toast.maketext(getactivity(), "Не удалось определить ваше местоположение, проверте ваши разрешения и настройки", toast.length_long).show(); } adapter.notifydatasetchanged();
to custom adapter send instance of database ad using in there , slows main tread. i`m trying use asynctask in adapter images path database , set images async absolutely nothing
package ua.com.it_artel.tvoy_golos.service;
public class pointslistarrayadapter extends baseadapter {
context context; private list<point> pointslist; private file appfolder; private string resoursesfolder; private yourvoicedatabasehelper dbhelper; private string imagepath; public pointslistarrayadapter(context context, list<point> pointslist, file appfolder, string resoursesfolder, yourvoicedatabasehelper dbhelper) { this.context = context; this.pointslist = pointslist; this.appfolder = appfolder; this.resoursesfolder = resoursesfolder; this.dbhelper = dbhelper; } @override public int getcount() { return pointslist.size(); } @override public object getitem(int position) { return pointslist.get(position); } @override public long getitemid(int position) { return pointslist.get(position).getid(); } static class viewholder { imageview image; textview adress; textview name; textview distance; location ellocation; point point; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder viewholder; view rowview = convertview; if(convertview == null){ layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); rowview = inflater.inflate(r.layout.list_element_layout_type_1, parent, false); viewholder = new viewholder(); viewholder.ellocation = new location(""); viewholder.image = (imageview) rowview.findviewbyid(r.id.item_image); viewholder.adress = (textview) rowview.findviewbyid(r.id.item_text); viewholder.name = (textview) rowview.findviewbyid(r.id.item_name); viewholder.distance = (textview) rowview.findviewbyid(r.id.item_distance); rowview.settag(viewholder); } else { viewholder = (viewholder) rowview.gettag(); } viewholder.ellocation.setlatitude(double.parsedouble(pointslist.get(position).getlatitude())); viewholder.ellocation.setlongitude(double.parsedouble(pointslist.get(position).getlongtitude())); viewholder.point = pointslist.get(position); new adapterimagesetter().execute(viewholder); viewholder.adress.settext(pointslist.get(position).getaddress()); viewholder.name.settext(dbhelper.getpartnerbyuid(pointslist.get(position).getpartneruid()).getname()); viewholder.distance.settext(string.valueof(math.round(mylocationlistener.mylocation.distanceto(viewholder.ellocation))) + " м"); return rowview; } private class adapterimagesetter extends asynctask<viewholder, void, viewholder> { @override protected viewholder doinbackground(viewholder... params) { viewholder viewholder = params[0]; imagepath = dbhelper.getpointspartnerimage(viewholder.point.getpartneruid()); if (imagepath.equals("")) { viewholder.image.setimageresource(r.drawable.no_image); } else { drawable dr = drawable.createfrompath(appfolder.getpath() + "/" + resoursesfolder + "/" + imagepath); viewholder.image.setimagedrawable(dr); } return viewholder; } }
}
i have tried different variations in asynctask , runnable doesn`t work, if there no code.. hope u understand english , give advice
you can not perform ui related operations in doinbackground method ui related operation use onpostexcecute method more asynch task http://developer.android.com/reference/android/os/asynctask.html
in code you setting imege
if (imagepath.equals("")) { viewholder.image.setimageresource(r.drawable.no_image); } else { drawable dr = drawable.createfrompath(appfolder.getpath() + "/" + resoursesfolder + "/" + imagepath); viewholder.image.setimagedrawable(dr); }
just add code in onpostexecute
Comments
Post a Comment