-2

I'm following the "How do I use Glide guide found here: https://github.com/bumptech/glide

I've got the "simple view" example working but I'm having a problem with the "Image List" example.

The example from GitHub show's the following code. I can't figure out what to replace "myFragment" with. I've tried "this" but it doesn't work. Any help on figuring out what to replace "myFragment" with would be greatly appreciated.

Here's the GitHub example...

// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
    final ImageView myImageView;
    if (recycled == null) {
        myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
    } else {
        myImageView = (ImageView) recycled;
    }

    String url = myUrls.get(position);

    Glide
        .with(myFragment)
        .load(url)
        .centerCrop()
        .placeholder(R.drawable.loading_spinner)
        .crossFade()
        .into(myImageView);

    return myImageView;
}

Here's the code from my android project...

I have a list view in my MainActivity. Each row in the list will be populated with a custom view. To do that I have build a custom layout and a custom ArrayAdapter.

Here's the Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_marginBottom="10dp"
            android:id="@+id/postUserFirstName"/>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:id="@+id/postPicture"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_marginBottom="10dp"
            android:id="@+id/postCaption"/>

    </LinearLayout>

</LinearLayout>

Here's the custom Array Adapter

public class CustomAdapter extends ArrayAdapter<Post> {

    // We'll define a context and we will create a list of type Post that will hold all of our Post objects
    private final Context context;
    private final List<Post> customPosts;

    //constructor
    public CustomAdapter(Context context, List<Post> list) {

        super(context, R.layout.custom_post_layout, list);
        this.context = context;
        this.customPosts = list;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //connect to custom_post_layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.custom_post_layout, parent, false);

        //assign views
        TextView postUserFirstName = (TextView) rowView.findViewById(R.id.postUserFirstName);
        ImageView postPicture = (ImageView) rowView.findViewById(R.id.postPicture);
        TextView postCaption = (TextView) rowView.findViewById(R.id.postCaption);

        //set values
        postUserFirstName.setText(customPosts.get(position).getUserFacebookName());
        Glide.with(this).load("https://example.jpg").into(postPicture);
        postCaption.setText(customPosts.get(position).getPostCaption());

        return rowView;
    }

}
Canadian
  • 31
  • 1
  • 9
  • 2
    Possible duplicate of [What is 'Context' on Android?](http://stackoverflow.com/questions/3572463/what-is-context-on-android) – Selvin Dec 15 '16 at 19:59

2 Answers2

0
Glide.with(context).load("https://example.jpg").into(postPicture);

Replace the myFragment with the context you get from the Activity or Fragment that calls the CustomAdapter constructor. That will do. Also, add a real image URL to check if the list loads the images or not. Note: You need to use the context object here instead of 'this' keyword because the Context object tells Glide what activity or fragment (indirectly the activity) is creating the event, and where the data is to be put while 'this' keyword simply references the current CustomAdapter class.

For example, let's consider you have an activity named MainActivity. Now in main activity you need to do this:

CustomAdapter adapter = new CustomAdapter(this, yourList);
yourListView.setAdapter(adapter);

If you have a fragment, let's say a fragment called MyFragment, you would do this:

CustomAdapter adapter = new CustomAdapter(getContext(), yourList);
yourListView.setAdapter(adapter);

Note: yourList is the list you might be having that you wish to give the ArrayAdapter as an input.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
  • Thank you! Ok, I'm quite new to Java, so I apologize in advance for another question. I have an activity calling the CustomAdapter constructor. How do I figure out what context I get from the activity? – Canadian Dec 15 '16 at 20:05
  • Would you be able to give me an example? Thanks. – Canadian Dec 15 '16 at 20:06
  • Updated the answer. Check out and let know if it helped. – Nilesh Singh Dec 15 '16 at 20:16
  • That is exactly what I am doing. My MainActivity is creating the custom adapter exactly as you have described. So now that I have that, what do I replace "context" with inside of Glide.with(context).load(https://example.jpg").into(postPicture); Thanks again for all of the help! I really appreciate this. – Canadian Dec 15 '16 at 20:29
  • Please make sure you choose the answer as the valid answer so that it might come handy to others in the community. Peace. – Nilesh Singh Dec 15 '16 at 20:34
  • Thank you. I just clicked the green check. Is that what you meant? – Canadian Dec 15 '16 at 20:42
  • Also, I'm still not sure what to put inside of Glide.with(???). How do I get the context of the calling activity? – Canadian Dec 15 '16 at 20:43
  • just put context inside it and everything else is already done by you. Also, don't worry about the context if you have already added the code provided above as that part is also done in the code. Run the ListView and check. – Nilesh Singh Dec 15 '16 at 20:45
  • Unfortunately, the images are still not loading. I've added a real image URL and nothing. – Canadian Dec 15 '16 at 20:53
  • Everything else is loading? I mean the text in textviews? – Nilesh Singh Dec 15 '16 at 20:54
  • Yes everything else (the text) is loading. – Canadian Dec 15 '16 at 21:04
  • Okay, then the code is fine. Have you added Internet permission in your manifest? Check the log for Glide as well and see what is restricting it from loading the image. – Nilesh Singh Dec 15 '16 at 21:08
  • The only thing showing up in the log under glide is... 12-16 13:11:45.682 878-3481/? I/ActivityManager: Start proc 2931:com.google.android.apps.maps/u0a59 for service com.google.android.apps.maps/com.google.android.apps.gmm.shared.cache.glide.GlideDiskCacheExpirationService – Canadian Dec 17 '16 at 01:13
0

I finally found the problem. The issue was in the xml layout file under ImageView.

android:layout_height="0dp"

Changing this to a fixed height - for example 200dp - fixed the problem.

Thanks for all of the help.

Canadian
  • 31
  • 1
  • 9