# Android RecyclerView Tutorial

**URL:** <https://forums.kodeco.com/t/android-recyclerview-tutorial/16023>\
**Category:** 🎓 Courses & Articles\
**Created:** [June 16, 2016, 1:03pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023 "2016-06-16T13:03:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![daemonic\_daz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daemonic_daz/32/108563_2.png) [@daemonic\_daz](https://forums.kodeco.com/u/daemonic_daz)\
**Post date:** [June 16, 2016, 1:03pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/1 "2016-06-16T13:03:07Z")

</div>

Learn how to display datasets of large or unknown size in this Android RecyclerView tutorial!

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/1092-android-recyclerview-tutorial](https://www.raywenderlich.com/1092-android-recyclerview-tutorial)

---

<div class="post-metadata">

**Author:** ![luxjahi](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@luxjahi](https://forums.kodeco.com/u/luxjahi)\
**Post date:** [October 11, 2016, 10:08pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/2 "2016-10-11T22:08:39Z")

</div>

where did you get receivedNewPhoto()??? Does this guide need an update? I am very confused there are a few issues where you ask to write code but the guide nor the studio give/create those areas. Maybe I am missing something.

---

<div class="post-metadata">

**Author:** ![yaizudamashii](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/yaizudamashii/32/45846_2.png) [@yaizudamashii](https://forums.kodeco.com/u/yaizudamashii)\
**Post date:** [November 8, 2016, 9:08pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/3 "2016-11-08T21:08:53Z")

</div>

receivedNewPhoto() is included in MainActivity of the starter code.

---

<div class="post-metadata">

**Author:** ![sutheres](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sutheres/32/52358_2.png) [@sutheres](https://forums.kodeco.com/u/sutheres)\
**Post date:** [December 22, 2016, 3:10pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/4 "2016-12-22T15:10:00Z")

</div>

None of the images are coming down from the API. The date and description works fine but for some reason the photos will not. The photos don’t load in the final version either, I downloaded and ran it just to be safe.

---

<div class="post-metadata">

**Author:** ![pronab](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pronab/32/88961_2.png) [@pronab](https://forums.kodeco.com/u/pronab)\
**Post date:** [December 31, 2016, 12:39pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/5 "2016-12-31T12:39:22Z")

</div>

The api returns the following :  
{  
“date”: “2016-12-31”,  
“explanation”: “The Trifid Nebula, also known as Messier 20, is easy to find with a small telescope, a well known stop in the nebula rich constellation Sagittarius. But where visible light pictures show the nebula divided into three parts by dark, obscuring dust lanes, this penetrating infrared image reveals filaments of glowing dust clouds and newborn stars. The spectacular false-color view is courtesy of the Spitzer Space Telescope. Astronomers have used the Spitzer infrared image data to count newborn and embryonic stars which otherwise can lie hidden in the natal dust and gas clouds of this intriguing stellar nursery. As seen here, the Trifid is about 30 light-years across and lies only 5,500 light-years away. News: Leap second to be added to 2016.”,  
“hdurl”: “[http://apod.nasa.gov/apod/image/1612/trifid\_spitzerR.jpg](http://apod.nasa.gov/apod/image/1612/trifid_spitzerR.jpg)”,  
“media\_type”: “image”,  
“service\_version”: “v1”,  
“title”: “Infrared Trifid”,  
“url”: “[http://apod.nasa.gov/apod/image/1612/trifid\_spitzerR1024.jpg](http://apod.nasa.gov/apod/image/1612/trifid_spitzerR1024.jpg)”  
}  
Picasso should be able to download using url in the json; However I confirm ,as is,it does not work for me either. Have to dissect the code .

---

<div class="post-metadata">

**Author:** ![mademma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mademma/32/64305_2.png) [@mademma](https://forums.kodeco.com/u/mademma)\
**Post date:** [December 31, 2016, 3:31pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/6 "2016-12-31T15:31:35Z")

</div>

> [@pronab](#):
>
> “url”: “[http://apod.nasa.gov/apod/image/1612/trifid\_spitzerR1024.jpg](http://apod.nasa.gov/apod/image/1612/trifid_spitzerR1024.jpg)”

I had to modify getUrl() to include return “[https://apod.nasa.gov/apod/image/1612/trifid\_spitzerR1024.jpg](https://apod.nasa.gov/apod/image/1612/trifid_spitzerR1024.jpg)” before it would work.

---

<div class="post-metadata">

**Author:** ![pronab](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pronab/32/88961_2.png) [@pronab](https://forums.kodeco.com/u/pronab)\
**Post date:** [December 31, 2016, 3:58pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/7 "2016-12-31T15:58:10Z")

</div>

@mademma but…it does that already ?..in Photo… public String getUrl() {  
return mUrl;  
}

only issue I could see is the media\_type should be image not video, however that is still not working,  
but did yours work?

---

<div class="post-metadata">

**Author:** ![mademma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mademma/32/64305_2.png) [@mademma](https://forums.kodeco.com/u/mademma)\
**Post date:** [December 31, 2016, 7:06pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/8 "2016-12-31T19:06:10Z")

</div>

I didn’t explain it very good. mUrl returns [http://apod.nasa.gov/apod/image/1612/trifid\_spitzerR1024.jpg](http://apod.nasa.gov/apod/image/1612/trifid_spitzerR1024.jpg)" before it would work. I modified mUrl to contain [https://apod.nasa.gov/apod/image/1612/trifid\_spitzerR1024.jpg](https://apod.nasa.gov/apod/image/1612/trifid_spitzerR1024.jpg)" before turning it. (added the “s”).

Also, I did change the media\_type to image.

I also changed ImageRequester onResponce as follows:

From: if (!photoJSON.getString(MEDIA\_TYPE\_KEY).equals(MEDIA\_TYPE\_VIDEO\_VALUE)) {  
To: if (photoJSON.getString(MEDIA\_TYPE\_KEY).equals(MEDIA\_TYPE\_VIDEO\_VALUE)) {

I haven’t had a chance to really look into that change and why it worked.

---

<div class="post-metadata">

**Author:** ![pronab](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pronab/32/88961_2.png) [@pronab](https://forums.kodeco.com/u/pronab)\
**Post date:** [January 1, 2017, 10:16am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/9 "2017-01-01T10:16:25Z")

</div>

@mademma Thanks , yeah it works now,kind of, also I removed the date qualifier in the url with the api key…it was not liking the new year…🕶

---

<div class="post-metadata">

**Author:** ![yeonku](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/yeonku/32/81494_2.png) [@yeonku](https://forums.kodeco.com/u/yeonku)\
**Post date:** [January 2, 2017, 5:08am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/10 "2017-01-02T05:08:35Z")

</div>

Hi, everyone.

You can simply change one line.

in Photo.java line 46 :

`mUrl = photoJSON.getString("url");`

To.

`mUrl = photoJSON.getString("url").replace("http://", "https://");`

Happy new Year. 🎁

---

<div class="post-metadata">

**Author:** ![zhaoyou](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zhaoyou/32/38282_2.png) [@zhaoyou](https://forums.kodeco.com/u/zhaoyou)\
**Post date:** [January 13, 2017, 2:31pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/11 "2017-01-13T14:31:47Z")

</div>

Thanks, Great Tutorial !

---

<div class="post-metadata">

**Author:** ![nriebesel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nriebesel/32/55964_2.png) [@nriebesel](https://forums.kodeco.com/u/nriebesel)\
**Post date:** [January 30, 2017, 1:44pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/12 "2017-01-30T13:44:57Z")

</div>

Hi, I’m getting a Null Pointer Exception. Did I miss something?

`java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference at com.raywenderlich.galacticon.RecyclerAdapter.getItemCount(RecyclerAdapter.java:82)`

Thanks for the help and the tutorial.

---

<div class="post-metadata">

**Author:** ![chris\_l](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/chris_l/32/73046_2.png) [@chris\_l](https://forums.kodeco.com/u/chris_l)\
**Post date:** [January 31, 2017, 4:14am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/13 "2017-01-31T04:14:12Z")

</div>

I got this too. It’s because I forgot to implement the constructor for mPhotos in the RecyclerAdapter.

At the top of your RecyclerAdapter class, add a variable to hold your photos:

```auto
private ArrayList<Photo> mPhotos;

```

Next, add a constructor to set it up:

```auto
    public RecyclerAdapter(ArrayList<Photo> photos) {
        mPhotos = photos;
    }

```

---

<div class="post-metadata">

**Author:** ![nriebesel](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nriebesel/32/55964_2.png) [@nriebesel](https://forums.kodeco.com/u/nriebesel)\
**Post date:** [January 31, 2017, 9:25am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/14 "2017-01-31T09:25:16Z")

</div>

Thanks for your answer. I checked it but variable and constructor is set just like in the tutorial.

This is the full log message:

> E/AndroidRuntime: FATAL EXCEPTION: main  
> Process: com.raywenderlich.galacticon, PID: 11787  
> java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.util.ArrayList.size()’ on a null object reference  
> at com.raywenderlich.galacticon.RecyclerAdapter.getItemCount(RecyclerAdapter.java:82)  
> at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2938)  
> at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2904)  
> at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)  
> at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1730)  
> at android.widget.LinearLayout.onLayout(LinearLayout.java:1496)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)  
> at android.widget.FrameLayout.onLayout(FrameLayout.java:261)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)  
> at android.widget.FrameLayout.onLayout(FrameLayout.java:261)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)  
> at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)  
> at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)  
> at android.widget.FrameLayout.onLayout(FrameLayout.java:261)  
> at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)  
> at android.view.View.layout(View.java:17637)  
> at android.view.ViewGroup.layout(ViewGroup.java:5575)  
> at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)  
> at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)  
> at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)  
> at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)  
> at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)  
> at android.view.Choreographer.doCallbacks(Choreographer.java:686)  
> at android.view.Choreographer.doFrame(Choreographer.java:621)  
> at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)  
> at android.os.Handler.handleCallback(Handler.java:751)  
> at android.os.Handler.dispatchMessage(Handler.java:95)  
> at android.os.Looper.loop(Looper.java:154)  
> at android.app.ActivityThread.main(ActivityThread.java:6119)  
> at java.lang.reflect.Method.invoke(Native Method)  
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  
> E/EGL\_emulation: tid 11809: eglSurfaceAttrib(1174): error 0x3009 (EGL\_BAD\_MATCH)  
> W/OpenGLRenderer: Failed to set EGL\_SWAP\_BEHAVIOR on surface 0x7a5c6180da00, error=EGL\_BAD\_MATCH  
> Application terminated.

I’m pretty much new to Android so I appreciate the help!

---

<div class="post-metadata">

**Author:** ![naozumi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/naozumi/32/58963_2.png) [@naozumi](https://forums.kodeco.com/u/naozumi)\
**Post date:** [March 2, 2017, 12:22am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/15 "2017-03-02T00:22:50Z")

</div>

The null pointer exception is likely caused by the code being in the wrong order:

> Next, underneath the creation of your array list in onCreate() add the following:  
> `mAdapter = new RecyclerAdapter(mPhotosList);`  
> `mRecyclerView.setAdapter(mAdapter);`

Make sure the order is correct - you must do the mAdapter work _after_ the ArrayList otherwise you get that exception:

```
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mLinearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);

    //These two lines first - they are in the starter project
    mPhotosList = new ArrayList<>();
    mImageRequester = new ImageRequester(this);

    //These two lines you must add as per description - after ArrayList
    mAdapter = new RecyclerAdapter(mPhotosList);
    mRecyclerView.setAdapter(mAdapter);
  }

```

---

<div class="post-metadata">

**Author:** ![naozumi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/naozumi/32/58963_2.png) [@naozumi](https://forums.kodeco.com/u/naozumi)\
**Post date:** [March 2, 2017, 12:36am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/16 "2017-03-02T00:36:05Z")

</div>

One other thing to note - NASA works to an American time zone (not exactly sure which one). This means that you may get an exception when asking for the API if the picture has not yet been published but your timezone is already moved to the next day - for example its 2nd March in UK but still 1st March in USA.

Under these circumstances the app will show in the android debugger (at start of a stacktrace):  
`W/System.err: org.json.JSONException: No value for media_type`

The JSON returned is:  
`{ "code": 500, "msg": "Internal Service Error", "service_version": "v1" }`

If you have not yet implemented the scrolling code yet you will not see any photo although the app is _technically_ operating correctly.

---

<div class="post-metadata">

**Author:** ![sandy1507](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sandy1507/32/102075_2.png) [@sandy1507](https://forums.kodeco.com/u/sandy1507)\
**Post date:** [March 3, 2017, 6:01am UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/17 "2017-03-03T06:01:40Z")

</div>

I have couple of question, could you please answer me?

1. How can I create a layout which contains both imageview and textview separately? For example say having an items conatining 12 items with 6 images and 6 texts

2. Can we have non-scrollable listview or gridview? I want the screen to be non scrollable and the list of items should fit the screen size.

---

<div class="post-metadata">

**Author:** ![purvandaru](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@purvandaru](https://forums.kodeco.com/u/purvandaru)\
**Post date:** [March 25, 2017, 12:59pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/18 "2017-03-25T12:59:18Z")

</div>

Hi, first sorry for my english bro haha  
i am a bit confused about how you save the photo object. Do you save it in ArrayList or something else, it’s really help if you can show me where the code to save the Photo Object. Thanks guys. And once again sorry for my english 🙂

---

<div class="post-metadata">

**Author:** ![abhay994](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/abhay994/32/66839_2.png) [@abhay994](https://forums.kodeco.com/u/abhay994)\
**Post date:** [May 12, 2017, 8:57pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/19 "2017-05-12T20:57:46Z")

</div>

I will show you how we can use a real-time database of google FireBase and map data with RecyclerView and cardView in Android.after connecting firebase database with the android studio. create RecyclerViewAdapter and pass the JSON of the firebase to the RecyclerViewAdapter.  
[http://www.programmingviral.com/android/firebase-recyclerview-tutorial/](http://www.programmingviral.com/android/firebase-recyclerview-tutorial/)

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 3, 2017, 5:55pm UTC](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023/20 "2017-07-03T17:55:06Z")

</div>

This tutorial is more than six months old, so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]

[Next page](https://forums.kodeco.com/t/android-recyclerview-tutorial/16023.md?page=2)
