我有用于显示图像的 json。我也动态创建imageView
. 我在 logcat 中获得了 JSON,并且还arrayList
从 JSON 中分离了图像。我想以动态创建的方式显示来自 JSON 的图像imageView
。
获取 JSON 的代码:
try{
ArrayList<NameValuePair> mNameValuePair = new ArrayList<NameValuePair>();
mNameValuePair.add(new BasicNameValuePair("id_product", "id_product"));
Log.i("NameValuePair","" + mNameValuePair);
JSONParser jparser = new JSONParser();
result = jparser.PostConnection(URL1, null);
Log.i("result",""+ result);
JSONArray jArray = new JSONArray(result);
Log.i("JSON ARRAY","" + jArray);
//
for(int i=0;i<jArray.length();i++){
Log.i("Json Length",""+ jArray.length());
//
JSONObject tableData = jArray.getJSONObject(i);
image = tableData.getString("image");
Log.i("imageinloop",""+ image);
arraylist.add(image);
// Log.i("ArrayList","" +arraylist);
// AddObjectToList(image);
}
addImagesToView();
Log.i("ArrayList","" +arraylist);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
动态创建imageView的方法。
public void addImagesToView() {
Log.i("ArrayList In Image","" +arraylist);
for (int i = 0; i < arraylist.size(); i++) {
imageButton = new ImageView(this);
DefaultHttpClient client = new DefaultHttpClient();
Bitmap bit = null;
try{
HttpResponse response = client.execute(new HttpGet(URL1));
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream in = entity.getContent();
bit = BitmapFactory.decodeStream(in);
Log.i("Bitmap Value",""+ bit);
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
imageButton.setImageBitmap(bit);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
LinearLayout.LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// for setting image margin and spacing(left,top,right,bottom)
params.setMargins(60, 20, 5, 5);
imageButton.setLayoutParams(params);
horizontalOuterLayouthome.addView(imageButton);
}
}