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
| package com.evaste.menumeric;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.androidexample.gridview.adapter.EntiteAdapter;
public class Entite extends Activity {
public static String categorie_label;
public static String categorie_id;
ImageButton magasiner;
ImageButton accueil;
ArrayList<Entities> enttiesList;
String position;
EntiteAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entite);
enttiesList = new ArrayList<Entities>();
new JSONAsyncTask()
.execute("http://www.evaste.com/manger/wsjsonent/jsonent");
ListView listview = (ListView) findViewById(R.id.entities);
adapter = new EntiteAdapter(getApplicationContext(), R.layout.row,
enttiesList);
listview.setAdapter(adapter);
Intent i = getIntent();
categorie_label = i.getStringExtra("categorie_label");
TextView categorieTitle = (TextView) findViewById(R.id.titre_pt);
categorieTitle.setText(categorie_label);
magasiner = (ImageButton) findViewById(R.id.magasiner);
magasiner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Entite.this, Magasiner.class);
startActivity(intent);
}
});
accueil = (ImageButton) findViewById(R.id.accueil);
accueil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Entite.this, MainActivity.class);
startActivity(intent);
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(Entite.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
// ------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("entite");
Log.e("---eded--", String.valueOf(jarray));
for (int i = 0; i < jarray.length(); i++) {
Intent j = getIntent();
categorie_id = j.getStringExtra("categorie_id");
String cat=categorie_id;
Log.e("---cat--", cat);
JSONObject object = jarray.getJSONObject(i);
Log.e("---obj--", String.valueOf(object));
String dd=object.getString("id_categorie");
Log.e("---dd--", String.valueOf(dd));
if(cat.equals(dd)){
Entities entities = new Entities();
entities.setEntitie_title(object
.getString("entitie_title"));
entities.setEntitie_description(object
.getString("entitie_description"));
entities.setEntitieprice(object
.getString("entitie_price"));
entities.setEntitie_cooking_time(object
.getString("entitie_cooking_time"));
entities.setImage(object.getString("entitie_image"));
enttiesList.add(entities);
}else{
Log.e("---obj--", "lklklklk");
}
}
return true;
}
// ------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getApplicationContext(),
"Unable to fetch data from server", Toast.LENGTH_LONG)
.show();
}
}
} |
Partager