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
| package com.example.projet_fin_etude;
import java.util.ArrayList;
import java.util.Arrays;
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.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 {
Place [] myList;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Intent intent = getIntent();
Bundle extras =intent.getExtras();
if (extras != null){
this.myList = (Place[]) extras.getParcelableArray("myList");
url="http://maps.googleapis.com/maps/api/distancematrix/json?origins="+myList[0].getLatitude()+","+myList[0].getLongitude()+"|"+"&destinations=";
for(int i=1;i<myList.length;i++)
{
url=url+myList[i].getLatitude()+","+myList[i].getLongitude()+"|";
}
url=url+"&mode=walking&language=fr-FR&sensor=true";
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 la liste de tous les employés
JSONArray results = json.getJSONArray(TAG_rows);
// parcourir toute la liste des employés
for(int i = 0; i < results.length(); i++){
// récupérer un employé de type JSONObject
JSONObject emp = results.getJSONObject(i);
// récupérer le JSONObject phone qui contient deux items
JSONObject elements = emp.getJSONObject(TAG_elements);
JSONObject dist = elements.getJSONObject(TAG_distance);
String value = dist.getString(TAG_value);
int val_dist=Integer.parseInt(value);
myList[i].setDistance(val_dist);
}
}
catch(JSONException e)
{
e.printStackTrace();
}
};
}.execute();
}
Arrays.sort(myList);
for(int i=0;i<myList.length;i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", String.valueOf(i));
map.put("name",myList[i].getNom());
map.put("adresse", myList[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) {
@SuppressWarnings("unchecked")
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