java - How to implement Loaders with dependency injection -
i using dependency injection in code create make code maintainable, started filling arraylist dummy data contained in string, started filling using database, use web api using json , using asynctask.
final arraylist<earthquake> earthquake = (arraylist<earthquake>) queryutils.extractearthquakes(); final arraylist<earthquake> earthquake = (arraylist<earthquake>) new earthquakecontroller(new earthquakesdb()).getlistofearthquakes(); final arraylist<earthquake> earthquake = (arraylist<earthquake>) new earthquakecontroller(new earthquakesjson()).getlistofearthquakes();
in order make work asynktask , independency injection in implemented method called execute.get() method.
here interface
public interface earthquakeinterface { list<earthquake> getearthqueakes(); }
here class implements interface
public class earthquakesjson extends asynctask<string,void,list<earthquake>>implements earthquakeinterface { private static string url_top_10_most_recent_earthqueake_above_6 = "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10"; @override public list<earthquake> getearthqueakes() { return this.execute(url_top_10_most_recent_earthqueake_above_6).get(); } .......................... ...................... ....................... }
now reading using loaders more efficient using asynctask, can´t figure how use loaders independency injection did asynctask.
this how did it, without using dependency injection although tried.
public class earthquakeloaderjson extends asynctaskloader<list<earthquake>> implements earthquakeinterface { private static string url_top_10_most_recent_earthqueake_above_6 = "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10"; public earthquakeloaderjson(context context){ super(context); } @override public list<earthquake> loadinbackground() { url url; httpurlconnection httpurlconnection = null; stringbuilder json = new stringbuilder(); string line=""; try { url = new url(url_top_10_most_recent_earthqueake_above_6); httpurlconnection = (httpurlconnection)url.openconnection(); httpurlconnection.setrequestmethod("get"); inputstream inputstream = httpurlconnection.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); while ((line=bufferedreader.readline())!= null){ json.append(line); } } catch (malformedurlexception e) { e.printstacktrace(); } catch(ioexception e){ e.printstacktrace(); } { if(httpurlconnection!=null){ httpurlconnection.disconnect(); } } // create empty arraylist can start adding earthquakes list<earthquake> earthquakes = new arraylist<>(); // try parse sample_json_response. if there's problem way json // formatted, jsonexception exception object thrown. // catch exception app doesn't crash, , print error message logs. try { jsonobject root = new jsonobject(json.tostring()); jsonarray features = root.getjsonarray("features"); (int = 0; < features.length(); i++) { jsonobject earthquake = features.getjsonobject(i); jsonobject properties = earthquake.getjsonobject("properties"); double mag = properties.getdouble("mag"); string place = properties.getstring("place"); long time = properties.getlong("time"); string mapurl = properties.getstring("url"); earthquakes.add(new earthquake(mag, place, time, mapurl)); } } catch (jsonexception e) { // if error thrown when executing of above statements in "try" block, // catch exception here, app doesn't crash. print log message // message exception. log.e("queryutils", "problem parsing earthquake json results", e); } // return list of earthquakes return earthquakes; } //this method implement interface @override public list<earthquake> getearthqueakes() { return null; }
here fragment needs list. code commented 1 couldn´t use once changed asynctask loaders.
public class listmainfragment extends fragment implements loadermanager.loadercallbacks<list<earthquake>> { public listmainfragment() { // required empty public constructor } listview listview; @override public view oncreateview(final layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view viewroot = inflater.inflate(r.layout.fragment_mainlist, container, false); //final arraylist<earthquake> earthquake = (arraylist<earthquake>) queryutils.extractearthquakes(); //final arraylist<earthquake> earthquake = (arraylist<earthquake>) new earthquakecontroller(new earthquakefromdb()).getlistofearthquakes(); //final arraylist<earthquake> earthquake = (arraylist<earthquake>) new earthquakecontroller(new earthquakesjson()).getlistofearthquakes(); //i tried use dependency injection // final arraylist<earthquake> earthquake = null;// (arraylist<earthquake>) new earthquakecontroller(new earthquakeloaderjson(getcontext())).getlistofearthquakes(); listview = (listview)viewroot.findviewbyid(r.id.list_view); /* earthquakeadapter noteadapter = new earthquakeadapter(getactivity(), earthquake); listview.setadapter(noteadapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { //final string mensaje = notas.get(position).getmag(); @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(intent.action_view); intent.setdata(uri.parse(earthquake.get(0).getmapurl())); log.w("listview", "se despliega la siguente url " + earthquake.get(0).getmapurl()); startactivity(intent); } });*/ getloadermanager().initloader(0,null,this).forceload(); return viewroot; } @override public loader<list<earthquake>> oncreateloader(int id, bundle args) { return new earthquakeloaderjson(getcontext()); } @override public void onloadfinished(loader<list<earthquake>> loader, list<earthquake> data) { final arraylist<earthquake> earthquakearraylist = (arraylist<earthquake>)data; earthquakeadapter earthquakeadapter = new earthquakeadapter(getactivity(),earthquakearraylist); listview.setadapter(earthquakeadapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { //final string mensaje = notas.get(position).getmag(); @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(intent.action_view); intent.setdata(uri.parse(earthquakearraylist.get(0).getmapurl())); startactivity(intent); } }); } @override public void onloaderreset(loader<list<earthquake>> loader) { }}
Comments
Post a Comment