java - How to make sure AsyncTask is executed -
i'm pretty new android development, feel have relatively simple question here , have managed tie down more complex parts overlook more simple bits. i've setup imageadapter class handles displaying images gridview in 1 of fragments. following tutorial displayed list of items in array.
i'm using asynctask populate arraylist, , converting arraylist standard array picasso can deal when displaying content.
my problem asynctask section of imageadapter not getting executed, imagearr[] picasso uses remaining empty.
how can make sure asynctask section of adapter executed?
i've tried this, doesn't seem working , think i'm little bit off...
public void oncreate() { new getprojects().execute(); } i've attached code bellow, appreciated!
note; servicehandler retrieving data @ url , turning string can parsed.
public class imageadapter extends baseadapter { //json url private static string url = "www.myjsonsourceurl.com"; //json nodes private static final string tag_logo = "logopath"; arraylist<string> imageurls = new arraylist<string>(); string[] imagearr = imageurls.toarray(new string[imageurls.size()]); private context mcontext; public imageadapter(context c) { mcontext = c; } public int getcount() { return imagearr.length; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } public void oncreate() { new getprojects().execute(); } // create new imageview each item referenced adapter public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { // if it's not recycled, initialize attributes imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(185, 185)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(3, 3, 3, 3); } else { imageview = (imageview) convertview; } picasso.with(mcontext).setindicatorsenabled(true); picasso.with(mcontext).setloggingenabled(true); picasso.with(mcontext).load(imagearr[position]).placeholder(r.drawable.ajaxloader).error(r.drawable.imageunavailable).into(imageview); return imageview; } // references our images //async task json making http call public class getprojects extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // nothing right } @override protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonarray json = new jsonarray(jsonstr); // looping through applications (int = 0; < json.length(); i++) { jsonobject p = json.getjsonobject(i); string logopath = p.getstring(tag_logo); imageurls.add(logopath); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); } } }
you need call in activity or fragment list defined not in adapter this. move asynctask class activity , call there.
asynctask gives methods onpreexecute() , onpostexecute() can toast message task started or completed. , should call setadapter() in onpostexecute() of class.
Comments
Post a Comment