0

In my app, I show data in a list view. It looks as below.

enter image description here

Each row in list view is different (ie., they have a different view structure). But every row is made up of 1 to many subviews (all subview are same ).

Problem: If try to inflate the rows from xml then I need to have xml with 1. 2 subviews 2. 3 subviews 3. 5 subviews and so on

If I try to build the rows dynamically then I wont be able to take advantage of the holder pattern or the views re-usability of Listview. And I will end up building the subviews if the number of subviews, in the returned view is less than required or deleting in case the subviews are more than required.

In a nutshell, how can I have different views in listview which does not hamper view re-usability or holder pattern ?

Edit:

I have extended ArrayAdapter and overriden getView, getViewTypeCount, getItemViewType

@Override
public int getItemViewType(int position) {
    return mListItems.get(position).getViewToBeRenderened().getViewId();
}

@Override
public int getViewTypeCount() {     
    return AppConstants.VwViewTypes.values().length;
}

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

    final VWListItem listItem = mListItems.get(position);
    if(listItem == null || listItem.getViewToBeRenderened() == null){
        return convertView;
    }       
    VWView renderingView = listItem.getViewToBeRenderened();

    if(convertView == null ){           
        convertView = renderingView.buildView(mContext.appContext);

    }

                ...
}

This approach worked initially when there were only 2-3 differing row views. For each of these row views i had a different xml layout and corresponding class which would build and inflate.

But now the number of such views is growing and I dont want to have a layout and class for each additional new row view. Instead I would like to reuse existing layouts and class and build on them. If there is a better solution then am open to it as well.

Jeevan
  • 7,674
  • 10
  • 45
  • 62

2 Answers2

4

when you have to manage different kinds of views you should override getViewTypeCount() and getItemViewType() and you will receive a number of convertViews equals to the int returned by getViewTypeCount(). For instance, If it returns 2 you will get 2 differents convertView.

Blackbelt
  • 148,780
  • 26
  • 271
  • 285
  • Did not get the last statement "It returns 2 null convertView". I have overriden those methods and am appropriately handling it. – Jeevan Jul 10 '13 at 09:16
  • then maybe I misunderstood your question ? – Blackbelt Jul 10 '13 at 09:19
  • Not explicitly. the item in the list, in the ArrayAdapter knows which view to build. I think i went wrong somewhere – Jeevan Jul 10 '13 at 09:27
  • Suppose you return two different kind of view with id 1 and 2. The first time getViewTypeCount returns 1 you get converView == null and inflate it, then everytime getViewTypeCount returns 1 you will get a convertView != null. The first Time getViewTypeCount returns 2 you will get again a new convertView == null – Blackbelt Jul 10 '13 at 09:30
  • True, thats how my app works. But I no where explicitly call getViewTypeCount or getItemViewType, I have overriden them for the android to pass appropriate convertview. – Jeevan Jul 10 '13 at 09:33
  • I see, you are using a sort of Factory design patter in order to build the view getView will returns? Very clever – Blackbelt Jul 10 '13 at 09:35
  • Yes, thanks :) But now the problem is new rows are coming and number of layout and classes are also increasing ! do you see anyway to handle it ? – Jeevan Jul 10 '13 at 09:36
  • No I am sorry I dont know. The Factory pattern imo is the best way to handle it. – Blackbelt Jul 10 '13 at 09:39
  • to add to the answer check this might help http://stackoverflow.com/questions/17370525/listview-adapter-with-arbitrary-number-of-row-types-dont-know-the-number-of-di/17370772#17370772 – Raghunandan Jul 10 '13 at 10:48
  • @Blackbelt: I am facing a related problem but with `RecyclerView` ... could you please help me out with my question [here](http://stackoverflow.com/q/34431734/3287204) ? – Y.S Dec 27 '15 at 10:19
0

ListView is supposedly used to relieve the coders of some burden when each row has similar repetitive pattern. Yes, for slight variations getViewType is the correct choice to implement, but I see a better solution to your problem: you should create your own linearlayout if you have a lot of customization requirement. Remember that listview itself is an expensive object to create. Create a custom linearlayout is not that hard.

Neoh
  • 14,891
  • 12
  • 62
  • 78
  • How ? at the end of the day I have to show item in list view, I have hundreds of results to show – Jeevan Jul 10 '13 at 09:24
  • I only saw you have 9 subviews in the image. If you create three separate listviews stacked in a linear layout vertically, will that not be your desired solution? – Neoh Jul 10 '13 at 09:30
  • No that wont work, sorry I guess from image it is not evident that i show data in listview – Jeevan Jul 10 '13 at 09:35