listview - Android: Where is the correct place to make a http request in a List Adapter -
i have list adapter listview in 1 activity,
now want populate list view data obtain web service. correct place execute async request server obtain json data populate adapter?
1: before instantiate adapter , passing through constructor? dont know how refreshing work.
2: in adapter?
2.1: in adapters constructor?
2.2 in view?
here adapter far:
public class mydevicesadapter extends baseadapter { private context mcontext; private string tag = "mydevicesadapter"; // keep objects in array private arraylist<mydevice> devices; /** * constructor creating devices list adapter * * @param context * context of calling class */ public mydevicesadapter(context context) { mcontext = context; devices = new arraylist<mydevice>(); } @override public int getcount() { return devices.size(); } @override public object getitem(int position) { return devices.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } /** * checks if list empty */ public boolean isempty() { return devices.isempty(); } @override public view getview(final int position, view convertview, viewgroup parent) { viewholder holder = null; layoutinflater minflater = (layoutinflater) mcontext .getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.my_device_item, null); holder = new viewholder(); holder.imageview = (imageview) convertview .findviewbyid(r.id.my_device_item_imageview); holder.txtname = (textview) convertview .findviewbyid(r.id.my_device_item_textview_name); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } // temp variable store current list item final mydevice deviceitem = (mydevice) getitem(position); //set image //set name //on click listener view convertview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { //log.d(tag, "deleting order in position : " + position); //deleteorder(orders.get(position), position, v); } }); return null; } /** * private view holder class keeps reference view * components. used increase performance of listview */ private class viewholder { imageview imageview; textview txtname; } }
here async request that's going fetch data:
network.login("mydevices",null, new asynchttpresponsehandler() { @override public void onsuccess(int statuscode, header[] headers, byte[] responsebody) { } @override public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) { // response failed :( } });
somewhere else ;)
based on app can trigger download position. should work in seperat thread or using asynctask. not block ui. if data available notify listeners calling notifydatasetchanged()
, done. ui been reloaded automatically after call.
Comments
Post a Comment