android - How to update a fragment from another class that isn't an fragment -
i'm beginner of android programming sorry if question seem silly need understand how update view class not activity or fragment. have created class fetch data google play services api. need redirect data fragment. common software design patterns achieve it?
this code unfortunately doesn't work
todayfragment
package com.salvo.weather.android.fragment; import android.os.bundle; import android.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import com.salvo.weather.android.r; import com.salvo.weather.android.app.volleycallback; import com.salvo.weather.android.entity.currentweatherentity; import com.salvo.weather.android.geolocation.currentgeolocation; /** * simple {@link fragment} subclass. */ public class todayfragment extends fragment { private static final string tag = todayfragment.class.getsimplename(); private currentgeolocation mcurrentgeolocation; public todayfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_today, container, false); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mcurrentgeolocation = currentgeolocation.get(getactivity()); } @override public void onstart() { super.onstart(); mcurrentgeolocation.getmgoogleapiclient().connect(); renderview(); } @override public void onpause() { super.onpause(); if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.stoplocationupdates(); } } @override public void onresume() { super.onresume(); if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.startlocationupdates(); } } @override public void onstop() { super.onstop(); if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.stoplocationupdates(); } } public void renderview() { // check if googleapiclient connected if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.getmcurrentweatherrequest().loaddata(new volleycallback() { @override public void onsuccess(currentweatherentity currentweatherentity) { log.i(tag, currentweatherentity.getmcity()); } }); } } }
currentgeolocation
package com.salvo.weather.android.geolocation; import android.content.context; import android.location.location; import android.os.bundle; import android.util.log; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.googleapiclient.connectioncallbacks; import com.google.android.gms.common.api.googleapiclient.onconnectionfailedlistener; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.salvo.weather.android.app.volleycallback; import com.salvo.weather.android.client.request.currentweatherrequest; import com.salvo.weather.android.entity.currentgeolocationentity; import com.salvo.weather.android.entity.currentweatherentity; /** * created mazzy on 30/05/15. */ public class currentgeolocation implements connectioncallbacks, onconnectionfailedlistener, locationlistener { private static final string tag = currentgeolocation.class.getsimplename(); // setting constants location private static final long update_interval_in_milliseconds = 10000; //ms private static final long fastest_update_interval_in_milliseconds = update_interval_in_milliseconds / 2; private static currentgeolocation scurrentgeolocation; private boolean mrequestinglocationupdates; private context mappcontext; private googleapiclient mgoogleapiclient; private locationrequest mlocationrequest; private currentgeolocationentity mcurrentgeolocationentity; private currentweatherrequest mcurrentweatherrequest; public static currentgeolocation get(context appcontext) { if (scurrentgeolocation == null) { scurrentgeolocation = new currentgeolocation(appcontext.getapplicationcontext()); } return scurrentgeolocation; } private currentgeolocation(context appcontext) { mappcontext = appcontext; mrequestinglocationupdates = true; mcurrentgeolocationentity = new currentgeolocationentity(); mcurrentweatherrequest = currentweatherrequest.get(appcontext); buildgoogleapiclient(); } @override public void onconnected(bundle bundle) { log.i(tag, "connected goggleapiclient"); if (mcurrentgeolocationentity.getmlastlocation() == null) { mcurrentgeolocationentity.setmlastlocation(locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient)); mcurrentweatherrequest.setmcurrentgeolocationentity(mcurrentgeolocationentity); } if (mrequestinglocationupdates) { startlocationupdates(); } } @override public void onconnectionsuspended(int i) { // connection google play services lost reason. call connect() // attempt re-establish connection. log.i(tag, "connection suspended"); mgoogleapiclient.connect(); } @override public void onconnectionfailed(connectionresult connectionresult) { // refer javadoc connectionresult see error codes might returned in // onconnectionfailed. log.i(tag, "connection failed: error " + connectionresult.geterrorcode()); } @override public void onlocationchanged(location location) { // update location mcurrentgeolocationentity.setmlastlocation(location); } public googleapiclient getmgoogleapiclient() { return mgoogleapiclient; } public void startlocationupdates() { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } public void stoplocationupdates() { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } public currentweatherrequest getmcurrentweatherrequest() { return mcurrentweatherrequest; } private synchronized void buildgoogleapiclient() { log.i(tag, "building google api client"); mgoogleapiclient = new googleapiclient.builder(mappcontext) .addconnectioncallbacks(this) .addapi(locationservices.api) .build(); createlocationrequest(); } private void createlocationrequest() { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(update_interval_in_milliseconds); mlocationrequest.setfastestinterval(fastest_update_interval_in_milliseconds); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } }
you must create listener
.
todayfragment
package com.salvo.weather.android.fragment; import android.os.bundle; import android.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import com.salvo.weather.android.r; import com.salvo.weather.android.app.volleycallback; import com.salvo.weather.android.entity.currentweatherentity; import com.salvo.weather.android.geolocation.currentgeolocation; /** * simple {@link fragment} subclass. */ public class todayfragment extends fragment implements currentgeoloaction.onupdatelistener { private static final string tag = todayfragment.class.getsimplename(); private currentgeolocation mcurrentgeolocation; public todayfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_today, container, false); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mcurrentgeolocation = currentgeolocation.get(getactivity()); mcurrentgeolocation.setonupdatelistener(this); } @override public void onstart() { super.onstart(); mcurrentgeolocation.getmgoogleapiclient().connect(); renderview(); } @override public void onpause() { super.onpause(); if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.stoplocationupdates(); } } @override public void onresume() { super.onresume(); if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.startlocationupdates(); } } @override public void onstop() { super.onstop(); if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.stoplocationupdates(); } } @override public void onupdate() { renderview(); } public void renderview() { // check if googleapiclient connected if (mcurrentgeolocation.getmgoogleapiclient().isconnected()) { mcurrentgeolocation.getmcurrentweatherrequest().loaddata(new volleycallback() { @override public void onsuccess(currentweatherentity currentweatherentity) { log.i(tag, currentweatherentity.getmcity()); } }); } } }
currentgeolocation
package com.salvo.weather.android.geolocation; import android.content.context; import android.location.location; import android.os.bundle; import android.util.log; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.googleapiclient.connectioncallbacks; import com.google.android.gms.common.api.googleapiclient.onconnectionfailedlistener; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.salvo.weather.android.app.volleycallback; import com.salvo.weather.android.client.request.currentweatherrequest; import com.salvo.weather.android.entity.currentgeolocationentity; import com.salvo.weather.android.entity.currentweatherentity; /** * created mazzy on 30/05/15. */ public class currentgeolocation implements connectioncallbacks, onconnectionfailedlistener, locationlistener { private static final string tag = currentgeolocation.class.getsimplename(); // setting constants location private static final long update_interval_in_milliseconds = 10000; //ms private static final long fastest_update_interval_in_milliseconds = update_interval_in_milliseconds / 2; private static currentgeolocation scurrentgeolocation; private boolean mrequestinglocationupdates; private context mappcontext; private googleapiclient mgoogleapiclient; private locationrequest mlocationrequest; private currentgeolocationentity mcurrentgeolocationentity; private currentweatherrequest mcurrentweatherrequest; private onupdatelistener monupdatelistener; public interface onupdatelistener { public void onupdate(); } public void setonupdatelistener(fragment todayfragment) { this.monupdatelistener = (onupdatelistener) todayfragment; } public static currentgeolocation get(context appcontext) { if (scurrentgeolocation == null) { scurrentgeolocation = new currentgeolocation(appcontext.getapplicationcontext()); } return scurrentgeolocation; } private currentgeolocation(context appcontext) { mappcontext = appcontext; mrequestinglocationupdates = true; mcurrentgeolocationentity = new currentgeolocationentity(); mcurrentweatherrequest = currentweatherrequest.get(appcontext); buildgoogleapiclient(); } @override public void onconnected(bundle bundle) { log.i(tag, "connected goggleapiclient"); if (mcurrentgeolocationentity.getmlastlocation() == null) { mcurrentgeolocationentity.setmlastlocation(locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient)); mcurrentweatherrequest.setmcurrentgeolocationentity(mcurrentgeolocationentity); } if (mrequestinglocationupdates) { startlocationupdates(); monupdatelistener.onupdate(); } } @override public void onconnectionsuspended(int i) { // connection google play services lost reason. call connect() // attempt re-establish connection. log.i(tag, "connection suspended"); mgoogleapiclient.connect(); } @override public void onconnectionfailed(connectionresult connectionresult) { // refer javadoc connectionresult see error codes might returned in // onconnectionfailed. log.i(tag, "connection failed: error " + connectionresult.geterrorcode()); } @override public void onlocationchanged(location location) { // update location mcurrentgeolocationentity.setmlastlocation(location); } public googleapiclient getmgoogleapiclient() { return mgoogleapiclient; } public void startlocationupdates() { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } public void stoplocationupdates() { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } public currentweatherrequest getmcurrentweatherrequest() { return mcurrentweatherrequest; } private synchronized void buildgoogleapiclient() { log.i(tag, "building google api client"); mgoogleapiclient = new googleapiclient.builder(mappcontext) .addconnectioncallbacks(this) .addapi(locationservices.api) .build(); createlocationrequest(); } private void createlocationrequest() { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(update_interval_in_milliseconds); mlocationrequest.setfastestinterval(fastest_update_interval_in_milliseconds); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } }
Comments
Post a Comment