0

I am displaying a list video episodes in a ListView. For this, I created a subclass of ArrayAdapter and passed it a list of all the episodes. This works just fine.

Now, I want to add three additional items to the list, because the list should start with an item describing the show to which the episodes belong. I also add two additional headers (one for the description of the show and then one before the episodes start).

So effectively my ListView should display episodeList.size()+3 items. My question is if I have to fake these three items in the episodeList and insert three dummy items at the beginning of the list to tell the ArrayAdapter that it should display my desired number of items, or is there a less hackish way of doing it?

nburk
  • 22,409
  • 18
  • 87
  • 132
  • see if this is a solution. make a new object of your subclass which is equal to previous object and add 3 items in it. now set this object for your listview. – Ronn Wilder Jun 23 '14 at 07:42

4 Answers4

1

You can acheive this through Listview with sections. here the tutorial

https://code.google.com/p/android-amazing-listview/

  • Using a third-party component wouldn't be my preferred way, I am asking if there is way to adjust the behaviour of `ListView` and `ArrayAdapter` to reach my goal, not for workarounds... – nburk Jun 23 '14 at 07:53
0

A simple way of doing it would be to add these three views you want to add on top of the listview on the view you inflate for the other items with the visibility attribute set to GONE, then in the adapter in case the position is 0 you set their visibility to VISIBLE.

AMC
  • 130
  • 8
  • That seems quite hackish too, isn't there are more elegant way to do so, or is the interaction of `ListView` and `ArrayAdapter` really that static and not customisable? – nburk Jun 23 '14 at 07:51
  • AFAIK yes it is (static). A better solution (even tho uses the same approach) is to create two xml layouts for your items (let's say layout_header and layout_item) and inflate the first one (layout_header) if the position is 0. This would avoid to inflate a more complex view every time (and it would do it only when necessary). – AMC Jun 23 '14 at 07:57
  • Yes, I am doing this already. To be honest, from all the answer, adding three dummy episodes in the beginning still seems like the simplest approach to the problem... I thought there might have been a way to directly tell the `ArrayAdapter` that I am going to need three mode items in the beginning, the only simple way to do so seems by adding three dummy items. Everything else is a lot more overhead since I have to deal with customizing of the views which I don't want. – nburk Jun 23 '14 at 08:05
0

you can add headerview so bind coustom layout as headerlayout of listview..so you have scroll same as listview... and you don't need to bind fake items in arraylist.

View headerview = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).in    flate(R.layout.description_and_start_view, null, false);
listview.addHeaderView(headerview);

Follow this

Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
0

You can use a layout containing all the 3 extra views and add that layout as headerview of the listview.Here is a good tutorial for that. I will recommend this method is your additional views use different data comparing to the listview items

Also you can modify the adapter to inflate multiple layout based on the position of the item. this link will show you how to add multiple layouts in the same listview.

Community
  • 1
  • 1
SathMK
  • 1,171
  • 1
  • 8
  • 18