0

I'm getting images url from my php page with JSON parse and i need insert the image to list view, but i dont know how i can do that. Any one can help me? My code here:

            // Getting JSON from URL
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json1 =  jParser.makeHttpRequest(url_all_products,"GET",params);

             Log.d("All Products: ", json1.toString());
                try {
                        products = json1.getJSONArray(TAG_PRODUCTS);

                        // looping through All Products
                        for (int i = 0; i < products.length(); i++) {
                            JSONObject c = products.getJSONObject(i);
                          String name = c.getString(TAG_NAME);
             String url = c.getString(TAG_URL);

                HashMap<String, String> map = new HashMap<String, String>();
              map.put(TAG_URL, url);
              map.put(TAG_NAME, name);
              productsList.add(map);
              ListAdapter adapter = new SimpleAdapter(
                     getActivity(), productsList,
                      R.layout.items, new String[] {
                              TAG_NAME},
                      new int[] { R.id.name });
              ListView list = (ListView) getView().findViewById(R.id.list);
              list.setAdapter(adapter);





                        }
            } catch (JSONException e) {
              e.printStackTrace();
            }
2
  • use Android-Universal-Image-Loader
    – sakir
    Jun 11, 2014 at 16:46
  • i dont know how, i need example
    – Aurimas_12
    Jun 11, 2014 at 16:49

2 Answers 2

1

Its better you use Lazyloading

You can either use thest1/LazyList

or Advance image caching Android-Universal-Image-Loader From here

Android-Universal-Image-Loader contain different examples to list images from url to list view.

After Editing

Your Main Functiom Where you Set Adaptor

In your code you are setting and initializing list view inside the for loop which is wrong,and you need to use custom adapter to list images using lazy loading.The below code is your code after i have edited some of its part.

 // Getting JSON from URL
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    // getting JSON string from URL
    JSONObject json1 =  jParser.makeHttpRequest(url_all_products,"GET",params);

     Log.d("All Products: ", json1.toString());
        try {
                products = json1.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);
                  String name = c.getString(TAG_NAME);
     String url = c.getString(TAG_URL);

        HashMap<String, String> map = new HashMap<String, String>();
      map.put(TAG_URL, url);
      map.put(TAG_NAME, name);
      productsList.add(map);





                }

//DON'T SET ADAPTER IN A LOOP
                MyListViewAdaptor adapter = new MyListViewAdaptor(
                        getActivity(), productsList,
                         );
                 ListView list = (ListView) getView().findViewById(R.id.list);
                 list.setAdapter(adapter);

    } catch (JSONException e) {
      e.printStackTrace();
    }

Write A custum Adaptor to display your images to a list view other than SimpleAdapter and use the Function used in lazyloading in the adpator and display the image.Let Make a Adapter MyListViewAdaptor.The example is given below

MyListViewAdaptor.java

public class MyListViewAdaptor extends BaseAdapter {

// Declaring Variables

private Activity activity;
private ArrayList<HashMap<String, String>> myarray;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
   // PLAESE READ ImageLoader IS A CLASS FROM LIB thest1/LazyList LAZYLOADING


public MyListViewAdaptor(Activity a, ArrayList<HashMap<String, String>> myarray) {

    activity = a;

    this.myarray = myarray;

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    imageLoader = new ImageLoader(activity);


}

public int getCount() {

    return myarray.size();

}

public Object getItem(int position) {

    return position;

}

public long getItemId(int position) {

    return position;

}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)

        // Get the view from mainpage_row.xml

        vi = inflater.inflate(R.layout.yourlistviewrowname, null);


    ImageView userImage = (ImageView) vi
            .findViewById(R.id.youtimageviewname);
TextView textName = (TextView) vi.findViewById(R.id.yourtextviewname);

    HashMap<String, String> datamap = new HashMap<String, String>();
    datamap = myarray.get(position);

    textName.setText(datamap.get("yourTextKeyHere"));




    imageLoader.DisplayImage(datamap.get("yourImageKeyHere"), userImage);


    return vi;
}
}

don't forget to add the permissions

  <uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope this will help you

1
  • I'am using LazyList but i dont know how i can put all URL to this adapter.
    – Aurimas_12
    Jun 11, 2014 at 16:42
1

here is the adapter class

class ItemAdapter extends BaseAdapter {

        private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

        private class ViewHolder {
            public TextView text;
            public ImageView image;
        }

        @Override
        public int getCount() {
            return imageUrls.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final ViewHolder holder;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.item_list_image, parent, false);
                holder = new ViewHolder();
                holder.text = (TextView) view.findViewById(R.id.text);
                holder.image = (ImageView) view.findViewById(R.id.image);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.text.setText("Item " + (position + 1));

            imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener);

            return view;
        }
    }

and this is the main activity

public class ImageListActivity extends AbsListViewBaseActivity {

    DisplayImageOptions options;

    String[] imageUrls;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_image_list);

        Bundle bundle = getIntent().getExtras();
        imageUrls = bundle.getStringArray(Extra.IMAGES);

        options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error)
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .considerExifParams(true)
            .displayer(new RoundedBitmapDisplayer(20))
            .build();

        listView = (ListView) findViewById(android.R.id.list);
        ((ListView) listView).setAdapter(new ItemAdapter());
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startImagePagerActivity(position);
            }
        });
    }
}

**it is example from the offical site of the universal image loader

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.