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
| public class GetChoiceActivity extends ListActivity implements OnClickListener {
String city;
String field;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
List<HashMap<String, String>> centersList;
// url to get all products list
private static String url_all_products = "http://lien.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "centers";
private static final String TAG_PID = "_id";
private static final String TAG_NAME = "name";
private static final String TAG_TEL = "tel";
private static final String TAG_ADDRESS = "address";
private static final String TAG_FIELD = "field";
private static final String TAG_EMAIL = "email";
private static final String TAG_CITY = "city";
ArrayList<String> list;
// products JSONArray
JSONArray centers = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultat_list);
Intent i = getIntent();
city = i.getStringExtra(ResearchActivity.CITY);
field = i.getStringExtra(ResearchActivity.FIELD);
Toast.makeText(GetChoiceActivity.this, "city is: " + city + " field is: " + field, Toast.LENGTH_LONG).show();
// Hashmap for ListView
centersList = new ArrayList<HashMap<String, String>>();
registerForContextMenu(getListView());
// Loading products in Background Thread
new LoadAllCenters().execute();
protected String doInBackground(String... args) {
// Building Parameters
Log.d("detail choice ", "city is: " + city + " field is: " + field);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("city", city));
params.add(new BasicNameValuePair("field", field));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("Centers with selected options are: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
centers = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < centers.length(); i++) {
JSONObject c = centers.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String tel = c.getString(TAG_TEL);
String address = c.getString(TAG_ADDRESS);
String field = c.getString(TAG_FIELD);
String city = c.getString(TAG_CITY);
String email = c.getString(TAG_EMAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_TEL, tel);
map.put(TAG_ADDRESS, address);
map.put(TAG_CITY, city);
map.put(TAG_FIELD, field);
map.put(TAG_EMAIL, email);
// adding HashList to ArrayList
centersList.add(map);
list = new ArrayList<String>(map.values());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
GetChoiceActivity.this, centersList,
R.layout.list_item, new String[] {
TAG_NAME, TAG_TEL, TAG_EMAIL, TAG_ADDRESS, TAG_CITY, TAG_FIELD},
new int[] { R.id.textName,R.id.TextTel,R.id.TextEmail,R.id.TextAddress,R.id.TextCity,R.id.TextField });
// updating listview
setListAdapter(adapter);
}
});
}
@Override // Selection d'un item de la liste
protected void onListItemClick(ListView l, View v, int position, long id) {
//comment récupere l'id de l'item
v.showContextMenu(); |
Partager