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 167 168 169 170 171 172 173 174 175
| package activite;
public class List2 extends Activity implements OnItemClickListener{
protected ProgressDialog myProgressDialog;
final Handler uiThreadCallback = new Handler();
int i = 0;
String siret;
public final static String ITEM_TITLE = "title";
public final static String ITEM_CAPTION = "caption";
Button boutonTerminer;
ListSeparer adapter;
Map<String,String> item;
ArrayList<String> returnedValueNom = new ArrayList<String>();
ArrayList<Double> returnedValuePrix = new ArrayList<Double>();
ArrayList<Integer> returnedValueType = new ArrayList<Integer>();
public Map<String,?> createItem(String title, String caption) {
item = new HashMap<String,String>();
item.put(ITEM_TITLE, title);
item.put(ITEM_CAPTION, caption);
return item;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent thisIntent = getIntent();
siret = thisIntent.getExtras().getString("siret");
myProgressDialog = ProgressDialog.show(ListPlat2.this,"", "Chargement", true);
final Runnable runInUIThread = new Runnable() {
public void run() {
affiche_liste();
}
};
new Thread() {
@Override public void run() {
remplir_liste();
myProgressDialog.dismiss();
uiThreadCallback.post(runInUIThread);
}
}.start();
}
public void remplir_liste() {
getServerData();
String[] valueNom = returnedValueNom.toArray(new String[returnedValueNom.size()]);
Double[] valuePrix = returnedValuePrix.toArray(new Double[returnedValuePrix.size()]);
Integer[] valueType = returnedValueType.toArray(new Integer[returnedValueType.size()]);
adapter = new ListSeparer(this);
List<Map<String,?>> entree = new LinkedList<Map<String,?>>();
List<Map<String,?>> plat = new LinkedList<Map<String,?>>();
List<Map<String,?>> dessert = new LinkedList<Map<String,?>>();
i=0;
while(i < valueNom.length)
{
if(valueType[i]== 1)
{
entree.add(createItem(valueNom[i], (String.valueOf(valuePrix[i]))+" "));
}
i++;
}
adapter.addSection("Entrées", new SimpleAdapter(this, entree, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
i=0;
while(i < valueNom.length)
{
if(valueType[i]== 2)
{
plat.add(createItem(valueNom[i], (String.valueOf(valuePrix[i]))+" "));
}
i++;
}
adapter.addSection("Plats", new SimpleAdapter(this, plat, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
i=0;
while(i < valueNom.length)
{
if(valueType[i]== 3)
{
dessert.add(createItem(valueNom[i], (String.valueOf(valuePrix[i]))+" "));
}
i++;
}
adapter.addSection("Desserts", new SimpleAdapter(this, dessert, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
}
public void affiche_liste() {
boutonTerminer = new Button(this);
boutonTerminer.setText("Terminer la commande");
ListView list = new ListView(this);
list.addFooterView(boutonTerminer);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
this.setContentView(list);
}
public static final String strURL = "URL";
private void getServerData() {
InputStream is = null;
String result = "";
// CHOIX REQUETE :
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("siret", siret));
// CONNEXION :
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection ",e);
}
// CONVERSION REQUETE EN STRING
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result ",e);
}
// AFFICHAGE RESULTAT
try
{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
// Résultats de la requête : json_data.getInt("ID_ville")
//returnString += jArray.getJSONObject(i)+"\n";
returnedValueNom.add(json_data.getString("nom"));
returnedValuePrix.add(json_data.getDouble("prix"));
returnedValueType.add(json_data.getInt("type_plat"));
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data:",e);
}
}
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// TODO Auto-generated method stub
AjouterPanier dialog = new AjouterPanier(v.getContext(), item.get(ITEM_TITLE));
dialog.setTitle("Ajouter à la commande :");
dialog.show();
}
} |
Partager