Erez
Erez

Reputation: 1953

Populate android listview with list of objects with list in them

I'm trying to convert an IOS app to android and I'm having some issues with things that where very simple in IOS.

My issue right now is that i have a list of Chapters Each chapter is an object of:

{
    String
    int
    List<Paragraph>
}

I need to populate the listView with all the paragraphs for all the chapters and the string in the chapter need to be the header of the section so i will have a a display of

- Chapter header
-- Paragraph
-- Paragraph
-- Paragraph
- Chapter Header
-- Paragraph
-- Paragraph

And so on.... I already thought about creating a list of all the paragraphs from the chapters but then i loose the headers (the sections in my table) so this is no good. I am getting the correct list to the getView method in my adapter but need a way to get things right.

Any one has any idea how to do that in Android?

10x

Upvotes: 0

Views: 67

Answers (2)

Todor Kostov
Todor Kostov

Reputation: 1829

Another great approach is to use ExpandableListView. It's a bit more complicated than using a regular ListView or RecyclerView, but you will get the great functionality of expanding and collapsing your content based on the chapter headers.

You can check this article for a tutorial of how to implement it.

Upvotes: 1

Hassan Munir
Hassan Munir

Reputation: 449

Look you need to do this by using RecylerView rather with listview as it is latest version of listview. You need to write custom adapter with layout of two textview one for heading other for paragraph. Initialize this adapter with your list of objects then in onBinfViewHolder method get each object and show it in list.

Link to recycler view tutorial

Let me know if you required any other help.

Upvotes: 1

Related Questions