1, this method is very common, I think it is very practical, share it
public class Dirverdistancetool {
public void Getdirverdistance (latlng start, latlng end,final Ondirverdistancelistener ondirverdistancelistener) {
Routeplansearch Msearch = Routeplansearch.Newinstance ();
Msearch.setongetrouteplanresultlistener (New Ongetrouteplanresultlistener () {
Private Drivingrouteline Route;
public void Ongetwalkingrouteresult (Walkingrouteresult result) {}//Get the pedestrian route planning results
public void Ongettransitrouteresult (Transitrouteresult result) {}//Get bus transfer Path planning results
@Override
public void Ongetmasstransitrouteresult (Masstransitrouteresult masstransitrouteresult) {}
public void Ongetdrivingrouteresult (Drivingrouteresult result) {
if (Result.error = = Searchresult.errorno.No_error && NULL! = result && Null! = Result.getroutelines () && result.getroutelines (). Size () ; 0) {
int distance = Result.getroutelines (). Get (0). Getdistance ();
BigDecimal BigDecimal = new BigDecimal (Double.ValueOf (distance)/1000);
if (null! = Ondirverdistancelistener) Ondirverdistancelistener.dirverdistancelistener (Bigdecimal.setscale (1, BigDecimal.ROUND_HALF_UP). Doublevalue ());
}else{
System.out.println ("=============> Sorry, Span style= "font-family:" Arial "" Not calculated driving distance ... ");
if (null! = Ondirverdistancelistener) Ondirverdistancelistener.dirverdistancelistener (null);
}
}// Get driving route planning results
@Override
public void Ongetindoorrouteresult (Indoorrouteresult Indoorrouteresult) {}
@Override
public void Ongetbikingrouteresult (Bikingrouteresult bikingrouteresult) { }
});
Msearch.drivingsearch (New Drivingrouteplanoption ()). From (Plannode. Withlocation (Start)). to (Plannode. Withlocation (end)));
}
public interface ondirverdistancelistener{
void Dirverdistancelistener (Double RETURNKM);
}
}
Analysis:
This method is an interface callback socket callback, used to handle the management code, so that the code more layered sense
Routeplansearch Msearch = Routeplansearch.newinstance ();
In aOngetwalkingrouteresult();Ongettransitrouteresult ();
Ongetmasstransitrouteresult ();Ongetdrivingrouteresult()
Ongetindoorrouteresult();Ongetbikingrouteresult();
There are six methods if you put in a class will make the code very bloated, through the interface callback can solve this problem.
Write an interface that throws the Dirverdistancelistener () into the use class
public interface ondirverdistancelistener{
void Dirverdistancelistener (Double RETURNKM);
In the class used directly new, get the return of the RETURNKM,
(New Dirverdistancetool ()). Getdirverdistance (Start, End, new Dirverdistancetool.ondirverdistancelistener () {
@Override
public void Dirverdistancelistener (Double returnkm) {
if (null! = Deliveryprice && null! = RETURNKM &&!) Textutils.IsEmpty (minimun)) {
RETURNKM = RETURNKM;
Price = Deliveryprice * RETURNKM * (isweighthundred? Integer. valueOf (minimun): Textutils. IsEmpty (Other_t.gettext (). toString ())? 0:double. parsedouble (Other_t.gettext (). toString (). Replace (" ton", "")));
if (null! = Price)
Delivery_price_text.settext ((Price > 0)? "¥" + dubstring. keepprecision (Price + "", 2): "0.00");
}//end of If
}
});
2. This method is basic usage
Defining interfaces
Public interface Callbackgetaddress {
public void Editlistener (int position);
public void Deletelistener (int position);
}
By constructing a method to pass
Private Callbackgetaddress CallBack;
Public Addressadapter (context context, callbackgetaddress CallBack) {
This.context = context;
This.callback = CallBack;
}
Using callbacks
Vh.tvEdit.setOnClickListener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Callback.editlistener (i);
}
});
use direct implements addressadapter.callbackgetaddress in a class
rewrite
@Override
public void Editlistener (int position) {
Beanadresslist.address Address = list.get (position);
Bundle bundle = new bundle ();
Bundle.putserializable (adresseditactivity. Adressdetail, address);
Stephentoolutils. startactivitynofinish (activity, Adresseditactivity.class, Bundle, Url. editadress);
}
3. The third method of implementation, similar to the second one, is implemented by activity, which is relatively simple and rough
declaration Method
Private final publishactivity publishactivity;
Public Expadnableadapter (publishactivity publishactivity) {
this.publishactivity = publishactivity;
}
Put the method back in the past
Publishactivity.changeexpandablelistshow (Groupposition, isexpanded);
Use in activity
public void Changegroupprice (int groupposition, float price) {// sets whether the parent is edited, whether there are prices
Purchase_type.get (groupposition). Setediting (false);
Purchase_type.get (groupposition). Setcurprice (price);
}
Very practical, summary of individual projects
The necessary interface callback usage in Android