1

我有用于显示图像的 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);



        }
}
4

1 回答 1

0

使用 Universal Image Loader 它是很好的显示和缓存图像 https://github.com/nostra13/Android-Universal-Image-Loader

File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
 // Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
      // You can pass your own memory cache implementation
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
     .enableLogging()
     .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();

 ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
 imageLoader.displayImage(imageurl, image,options);
于 2013-11-13T04:59:09.560 回答