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
| package com.diablo;
import java.util.ArrayList;
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.Bundle;
import android.util.Log;
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.TextView;
public class InfoMapG extends ListActivity {
// url to make request
private static String url = "http://10.0.2.2/member.php";
// JSON Node names
private static final String TAG_Contact = "general";
private static final String TAG_IMEI = "IMEI";
private static final String TAG_id = "id";
private static final String TAG_Date = "Date";
private static final String TAG_Latitude = "Latitude";
private static final String TAG_Heure = "Heure";
private static final String TAG_Statut = "Statut";
private static final String TAG_Battery = "Battery";
private static final String TAG_Longitude = "Longitude";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infomapgenral);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_Contact);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_id);
String IMEI = c.getString(TAG_IMEI);
String Date = c.getString(TAG_Date);
String Latitude = c.getString(TAG_Latitude);
String Heure = c.getString(TAG_Heure);
String Longitude = c.getString(TAG_Longitude);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_id, id);
map.put(TAG_IMEI, IMEI);
map.put(TAG_Date, Date);
map.put(TAG_Longitude, Longitude);
map.put(TAG_Latitude, Latitude);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_IMEI, TAG_Date, TAG_Latitude, TAG_Longitude },new int[] {
R.id.IMEI, R.id.date, R.id.latitude, R.id.longitude });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// getting values from selected ListItem
String imei = ((TextView) view.findViewById(R.id.IMEI)).getText().toString();
String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
String latitude = ((TextView) view.findViewById(R.id.latitude)).getText().toString();
String longitude = ((TextView) view.findViewById(R.id.longitude)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItem.class);
in.putExtra(TAG_IMEI, imei);
in.putExtra(TAG_Date, date);
in.putExtra(TAG_Latitude, latitude);
in.putExtra(TAG_Longitude, longitude);
startActivity(in);
}
});
}
} |
Partager