1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| package com.example.projet_fin_etude;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Trie extends ListActivity {
ArrayList <Place> myList =new ArrayList <Place>();
private static final String TAG_rows = "rows";
private static final String TAG_elements = "elements";
private static final String TAG_distance= "distance";
private static final String TAG_value= "value";
String url;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
Intent intent = getIntent();
Bundle extras =intent.getExtras();
if (extras != null){
this.myList = (ArrayList<Place>) extras.getSerializable("myList");
double lt=Double.parseDouble(intent.getStringExtra("latitude"));
double lg=Double.parseDouble(intent.getStringExtra("longitude"));
url="https://maps.googleapis.com/maps/api/distancematrix/json?origins="+lt+","+lg+"&destinations=";
int i;
for( i=0;i<(myList.size())-1;i++)
{
url=url+myList.get(i).getLatitude()+","+myList.get(i).getLongitude()+"%7C";
}
url=url+myList.get(i).getLatitude()+","+myList.get(i).getLongitude()+"&mode=walking&language=fr-FR&sensor=true";
Log.d("test", url);
new AsyncTask<Void, Void, JSONObject>(){
@Override
protected JSONObject doInBackground(Void... params) {
// instancier la classe JsonParser
JsonParser jParser = new JsonParser ();
// récupérer le JSONObject à partir de lurl du fichier employes.json
JSONObject json = jParser.getJSONFromUrl(url);
return json;
};
protected void onPostExecute(JSONObject json) {
try {
// récupérer le tableau lignes
JSONArray lignes = json.getJSONArray(TAG_rows);
// récupérer le tableau elements
JSONObject elements = lignes.getJSONObject(0);
JSONArray results = elements.getJSONArray(TAG_elements);
// parcourir toute la liste des employés
for(int i = 0; i < results.length(); i++){
// récupérer un element de type JSONObject
JSONObject el = results.getJSONObject(i);
// récupérer le JSONObject Distance qui contient deux items
JSONObject dist = el.getJSONObject(TAG_distance);
String value = dist.getString(TAG_value);
Log.d("value", value);
int val_dist=Integer.parseInt(value);
Place p = new Place(myList.get(i).getReference(),myList.get(i).getNom(),myList.get(i).getAdresse(),myList.get(i).getLatitude(),myList.get(i).getLongitude(),val_dist);
myList.set(i,p);
}
Collections.sort(myList, Place.DST_COMPARATOR);
for(int i=0;i<myList.size();i++)
{
Log.d("objet",myList.get(i).toString());
}
}
catch(JSONException e)
{
e.printStackTrace();
}
};
}.execute();
}
for(int i=0;i<myList.size();i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", String.valueOf(i));
map.put("name",myList.get(i).getNom());
map.put("adresse", myList.get(i).getAdresse());
list.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, list , R.layout.activity_trie,
new String[] { "name", "adresse" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Trie.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_trie, menu);
return true;
}
} |
Partager