Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Naomi Touchet
Naomi Touchet
11,300 Points

How can I implement an infinitely scrolling list of items in my Android app?

I'm creating an app which will let people read through 1,000 different answers to commonly asked questions. I want them to be able to read through all of the answers (I was thinking it would work best to implement some sort of infinitely scrolling list) as well as to be able to read through the answers by category. Does anyone know the best way to do this? I would also appreciate if anyone knows any good resources to direct me towards.

I'm also wondering if I should use a database or if I should just use an array. (Right now I have it set up as an array, but I've only put in a couple of the answers. I'm not sure if an array will work once I have all 1,000 answers entered.)

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Naomi,

For the ListView itself, I'd recommend looking at a 3rd party library so you don't have to reinvent the wheel about making an infinite ListView. Check out this one, for example: https://github.com/weixiao1984/Android-Infinite-Scroll-Listview

As for your data, it sounds like a database might be appropriate for your answers. Our Data Persistence course is almost ready and should be able to help you. But an array might actually be just fine. There's no need to over-complicate it if an array is good enough for your purposes. An array of 1000 items just depends on the type of item being stored. The more complex the data object, the more memory it will take up.

Naomi Touchet
Naomi Touchet
11,300 Points

Thanks so much for your help Ben! The infinite ListView sounds like exactly what I need. Unfortunately I'm having problems importing the library into my workspace. I'm using Android Studio and I can't figure out how to get the library to work.

I copied and pasted the library from GitHub into my libs folder and then added compile files('libs/library') into the build.gradle file in the dependencies section. I also synchronized the library (right click, synchronize library) and tried cleaning and rebuilding the project. I'm still getting errors though like "Cannot resolve symbol 'InfiniteScrollListAdapter' " and I'm getting the same error for a couple other things like: InfiniteScrollListPageListener, LoadingMode, StopPosition.

Do you have any ideas on what might be going on? I thought I might just be importing the library the wrong way and so it's not being integrated with the program somehow but I can't figure out any other way to import the library.

Again, thank you so much for your help! I appreciate it.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Argh, I didn't realize that it wasn't updated for Gradle dependency management yet. This one might be easier to include if it still meets your needs: https://github.com/sedenardi/InfiniteScrollListView

It looks like you just need to include the 4 classes listed here: https://github.com/sedenardi/InfiniteScrollListView/tree/master/InfiniteScrollListView/src/main/java/com/sandersdenardi/infinitescrolllistview/app/lib

If you're still stuck after this, post the contents of your build.gradle file and I'll take a look.

Naomi Touchet
Naomi Touchet
11,300 Points

Great, I'll give that a try and let you know how it goes.

Naomi Touchet
Naomi Touchet
11,300 Points

Hey Ben, I'm not quite sure if it's working correctly or not. I included the 4 classes you listed in the libs folder--is that where they're supposed to be? Or should they be with the other java classes?

I don't have any errors but I couldn't really see a difference when I ran the program with and without the classes--it seems to be loading pretty fast either way.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

You should be able to add these four classes in your main package with your other classes.

I might have misinterpreted your question. Infinite ListViews are for things like Twitter, where each time you reach the bottom, more items are loaded. If you have a set number of items to load then you might be just fine with a regular ListView.

Naomi Touchet
Naomi Touchet
11,300 Points

Oh, okay. That makes sense--it seems like it's been loading fine so I probably just need the regular ListView. It was a good learning experience trying to figure out the Infinite ListViews anyways, maybe I'll use them on another project.

I have one more question--do you know if there's any way for the app to save a user's place in a regular ListView? So that if they leave the page they don't have to start all the way from the beginning.

Again, thanks so much for your help!

Naomi Touchet
Naomi Touchet
11,300 Points

Thanks! The video was great, unfortunately it doesn't seem to work in my project because I'm not using Parse. Is there a way to adapt that method so it doesn't need Parse?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

You bet! Paste in your code and we can help you finish it. The main idea is two parts:

  1. Add that null check (see the 2:15 mark) and call refill() if the adapter already exists
  2. Add the refill() method to your custom adapter (see the 2:45 mark)