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
| package com.pxr.tutorial.json;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.pxr.tutorial.xmltest.R;
public class Restaurant extends ListActivity {
String urlresto= "http://www.monresto.net/partenaires/restaurant/";
int width = 100;
int height = 200;
//TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);;
setContentView(R.layout.listplaceholder);
Button button = new Button(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
button.setText("Item name = " + bundle.getString("id")
+ " --- Go Back ");
} else {
button.setText("Go Back");
}
ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://monresto.net/android/marsa.php?id_cp="+ bundle.getString("id"));
try{
JSONArray monrestodb1 = json.getJSONArray("monrestodb1");
for(int i=0;i<monrestodb1.length();i++){
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject e = monrestodb1.getJSONObject(i);
map.put("name", String.valueOf(i));
map.put("name", "" + e.getString("nom"));
map.put("id", "" + e.getString("id"));
//map.put("photo",bitmap);
map.put("photo", "" + e.getString("photo"));
URL aURL = new URL("http://monresto.net/partenaires/restaurant/"+ e.getString("photo"));
Bitmap bitmap = BitmapFactory.decodeStream(aURL.openStream());
//bitmap.getScaledHeight(200);
//bitmap.getScaledWidth(100);
map.put("photo",bitmap);
//map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listmontplaisir,
new String[] { "name", "photo" },
new int[] { R.id.montplaisirtext , R.id.image });
adapter.setViewBinder(new MyViewBinder());
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(Restaurant.this, "NAME '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Restaurant.this, Famille_plat.class);
//intent.putExtra("url", "http://monresto.net/android/marsa.php");
String txt = o.get("id");
intent.putExtra("id", txt);
startActivity(intent);
}
});
}
} |
Partager