0

I want to Retrieve all of the images from the server via Glide or Volley library and show them in list view. How can I do it ?

4 Answers4

0

You don't need to do the image management. Glide can do it internally for you.

For ListView, use glide to load the image in getView() method of the adapter

For Recyclerview, use glide to load the image in onBindViewHolder() method of Adapter.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34
0

I want to Retrieve all of the images from the server via Glide or Volley library

Those libraries are used to loading & caching images on Android actually.

In order to download and show all of those images, you can make a Json output of all those links from server side by using json_encode() (on PHP for example) then parse the json in Android and loop through the whole Json output of links then show the images by Glide or Volley library.

Edit:

This is how it should be, on the Android(Client side) inside RecyclerView Adapter:

Glide.with(myContext)
                    .load(mPosts.imgurl)
                    .apply(options)
                    .into(ViewHolder.imgShow)

Follow: https://ledron.github.io/RecyclerView/

You can however add those links seperately inside an ArrayList on Android then showing those too. Follow the tutorial and use Adapter for RecyclerView.

Example Adapter:

public class Adapter  extends RecyclerView.Adapter<Adapter.ViewHolder>{

    ArrayList<String> urls;
    Context context;
    //constructor
    public Adapter(ArrayList<String> ImgUrl, Context context_)
    {
        this.urls = ImgUrl;
        this.context = context_;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        private ImageView image;

        public ViewHolder(View v)
        {
          super(v);
          image =(ImageView)v.findViewById(R.id.img);
        }

        public ImageView getImage(){ return this.image;}
    }

    @Override
    public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
        v.setLayoutParams(new RecyclerView.LayoutParams(1080,800));
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position)
    {
        Glide.with(this.context)
                .load(urls.get(position))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.getImage());
    }

    @Override
    public int getItemCount()
    {
        return urls.size();
    }

}
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Tnx, How I make a JSON output of all those links from the server side by using json_encode() (on PHP for example)? Can you tell me? –  Sep 24 '18 at 10:31
  • Just already told. You can make a list of URLs on the server side (Use Google) then use json_encode to give the json output. This is also related to another question and does not belong to this question. Please use Ask question for further questions. – ʍѳђઽ૯ท Sep 24 '18 at 10:56
  • Thanks, but I'm the beginner in android, I understand that I should "make a list of URLs on the server side (Use Google) then use json_encode to give the JSON output", but How do it? I want the code for it... –  Sep 26 '18 at 16:31
  • Is it a good question for asking: "In order to download and show all of the images, I want make a JSON output of all the links from server side by using json_encode() (on PHP for example) . tnx –  Sep 26 '18 at 16:41
  • @Mpi You can also add those links inside an `ArrayList` and showing them in the `RecyclerView` without being need to have the server side codes. But as you know, this is not a recommended way for the future links adding :) Check the updated answer. – ʍѳђઽ૯ท Sep 26 '18 at 16:41
  • Really? I will check it ... And I use eclipse for programming. –  Sep 26 '18 at 16:44
  • Yes, you can do that too. Use Android Studio. Eclipse is a depreciated IDE and fossil actually. However, like I said, **that's not a recommended way for the future links adding.** – ʍѳђઽ૯ท Sep 26 '18 at 16:45
  • Yes, I will change my IDE certainly, Okay, thanks. In this way that you said, Can I set an URL that shows the folder of my photos? for example www.example.com/photos –  Sep 26 '18 at 16:50
0

In Your Gradle

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

Load image into list

  String url = myUrls.get(position);

  GlideApp
    .with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .into(myImageView);
Anand Jain
  • 2,365
  • 7
  • 40
  • 66
0

Create a java class that Extends ArrayAdapter<ImageView> and use it for ListView:

class imageadapter extends ArrayAdapter<ImageView> {

    private Context context;
    private LayoutInflater inflater;

    private String[] imageurls;

    imageadapter(Context context, String[] imageurls) {
        super(context, R.layout.imagelayout);
        this.imageurls = imageurls;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }


    @SuppressLint({"ViewHolder", "SetTextI18n", "ClickableViewAccessibility"})
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View layout = convertView;
        Holder holder = null;
        if (layout == null) {
            layout = inflater.inflate(R.layout.imagelayout, parent, false);
            holder = new Holder();
            holder.imageView1 = layout.findViewById(R.id.imageView);
            holder.textView1 = layout.findViewById(R.id.textView);
            layout.setTag(holder);
        } else {
            holder = (Holder) layout.getTag();
        }

        Picasso.get().load(imageurls[position]).fit().into(holder.imageView1);
        holder.textView1.setText("Image:" + (position + 1));

        return layout;
    }

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


    static class Holder {

        ImageView imageView1;
        TextView textView1;

    }

}
Samir Alakbarov
  • 1,120
  • 11
  • 21