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
|
public class Rayon extends Activity {
private ContainerData container;
private Thread thread;
private Handler handler;
private ArrayList<MyDep> mesDep = new ArrayList<MyDep>();
private GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rayon);
gridview = (GridView) findViewById(R.id.gridview);
Log.i("", "begin parsing");
thread = new Thread(){
public void run(){
container = new ContainerData();
mesDep = container.parseDep();
handler.sendEmptyMessage(0);
};
};
thread.start();
Log.i("", "begin view");
handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 0) {
gridview.setAdapter(new ImageAdapter(Rayon.this, mesDep));
}
}
};
}
public class ImageAdapter extends BaseAdapter{
private class MyIcon{
TextView txt;
ImageView image;
}
private ArrayList<MyDep> mesDep = new ArrayList<MyDep>();
private LayoutInflater myInflater;
public ImageAdapter (Context context, ArrayList<MyDep> mesDep)
{
this.myInflater = LayoutInflater.from(context);
this.mesDep= mesDep;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.mesDep.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return this.mesDep.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
MyIcon icon;
Log.i("", "inflater");
if (arg1 == null)
{
arg1 = myInflater.inflate(R.layout.icon, null);
icon = new MyIcon();
icon.txt = (TextView) arg1.findViewById(R.id.viewDep);
icon.image = (ImageView) arg1.findViewById(R.id.imageIcon);
arg1.setTag(icon);
} else {
icon = (MyIcon) arg1.getTag();
}
Log.i("", "recuperer the path");
String sousChaine = mesDep.get(arg0).getImagePath().substring(15);
String sousChaine2 ="http://";
String url = sousChaine2.concat(sousChaine);
Bitmap b = telechargerImageBit(url);
Log.i("", url);
icon.image.setImageBitmap(Bitmap.createScaledBitmap(b, 60, 60, false));
Log.i("", "txt");
icon.txt.setText(mesDep.get(arg0).getName());
return arg1;
}
}
public static Bitmap telechargerImageBit(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bm;
}
} |
Partager