int jd=0;
String[]arKode;
double[]arJarak;
String[]arNama;
String[]arKet;
JSONParser jParser = new JSONParser(); JSONArray myJSON = null;
String myLati = "-6.18534";
String myLongi = "106.77894";
String myPosisi = "Posisi GPS Anda";
String ip="",id_cabang="CBG01",harga0="0";
int sukses;
Spinner spincabang;
String[]listSpinner;
String[]IDSpinner;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
{
......
ip = jParser.getIP();
new getCabang().execute();
}
class getCabang extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(ip+"cabang/cabang_show.php", "GET", params);
Log.d("show: ", json.toString());
try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
myJSON = json.getJSONArray(TAG_record);
jd=myJSON.length();
arNama=new String[jd];
arKet=new String[jd];
arKode=new String[jd];
arJarak=new double[jd];
double slat=0;
double slon=0;
try{
slat=Double.parseDouble(myLati);
slon=Double.parseDouble(myLongi);
}
catch(Exception ww){
}
for (int i = 0; i < jd; i++) {
JSONObject c = myJSON.getJSONObject(i);
String id_cabang = c.getString("id_cabang");
String atas = c.getString("atas");
String bawah = c.getString("bawah");
String lat = c.getString("latitude");
String lon = c.getString("longitude");
double dlat=0;
double dlon=0;
double jarak=0;
try{
dlat=Double.parseDouble(lat);
dlon=Double.parseDouble(lon);
jarak=jParser.getJarak(slat,slon,dlat,dlon);
}
catch(Exception ww){
}
arKode[i]=id_cabang;
arNama[i]=atas;
arKet[i]=bawah;
arJarak[i]=jarak;
}
int j;
boolean flag = true;
double temp=0.0;
String stemp="";
while ( flag ){
flag= false;
for( j=0; j < jd -1; j++ ){
if ( arJarak[ j ] > arJarak[j+1] ) {
temp = arJarak[ j ];
arJarak[ j ] = arJarak[ j+1 ];
arJarak[ j+1 ] = temp;
stemp=arKode[j];
arKode[ j ] = arKode[ j+1 ];
arKode[ j+1 ] = stemp;
stemp=arNama[j];
arNama[ j ] = arNama[ j+1 ];
arNama[ j+1 ] = stemp;
stemp=arKet[j];
arKet[ j ] = arKet[ j+1 ];
arKet[ j+1 ] = stemp;
flag = true;
}
}
}
listSpinner=new String[jd];
IDSpinner=new String[jd];
for (int i = 0; i < jd; i++) {
listSpinner[i]=arNama[i]+" ("+String.valueOf(arJarak[i])+" Km)";
IDSpinner[i]=arKode[i];
}
}
}
catch (JSONException e) {e.printStackTrace();}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
showSpinner();
}
});}
}
void showSpinner(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DetailJasa.this, android.R.layout.simple_spinner_item, listSpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spincabang.setAdapter(adapter);
spincabang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
//String item = parent.getItemAtPosition(position).toString();
//Log.d("Tag", item);
Toast.makeText(DetailJasa.this, "Anda Memilih Cabang "+listSpinner[position], Toast.LENGTH_SHORT).show();
id_cabang=IDSpinner[position];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public static double getJarak(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
int meterConversion = 1609;
double myjr=dist * meterConversion;
return Math.floor(myjr/1000);
}pada XML<Spinner
android:id="@+id/spincabang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
Tidak ada komentar:
Posting Komentar