0

I have 3 array list which gets data from various model classes.. I want to display these 3 in a single listview with separate headers. How can I achieve this? Can anyone help me?

Dilipchandar
  • 89
  • 1
  • 12

2 Answers2

0

You should learn how to use section.

You can read this answer on SO. Or directly follow these tutorial (also present in the original answer).

In your case, you should have one adapter with three different datas array. Let's call them array1, array2 and array3.

You should have the following getCount method :

public int getCount() {
     return array1.length + array2.length + array3.length + 3;//3 for 3 header
}

Then you should have 4 item view type (one for each array and one for the headers)

public int getItemViewTypeCount() {
     return 4
}

Then you should tell your adapter wich view type for each position

public int getItemViewType(int position) {
     if (position == 0) {
           return VIEW_TYPE_HEADER;
     } else if (position < 1 + array1.length) {
           return VIEW_TYPE_1;
     } else if (position == 1 + array1.length) {
            return VIEW_TYPE_HADER;
     } else if (position < 2 + array1.length + array2.length) {
           return VIEW_TYPE_2;
     } else if (position == 2 + array1.length + array2.length) {
            return VIEW_TYPE_HADER;
     } else if (position < 3 + array1.length + array2.length + array3.length) {
           return VIEW_TYPE_3;
     } else {
            return 0;//this should never happened
     }
}

Now you can implement getView

public View getView(int position, View convertView, ViewGroup parent) {
      switch (getItemViewType(position)) {
            case VIEW_TYPE_HEADER:
                    getHeaderView(positin, convertView, parent);
                    break;
            case VIEW_TYPE_1:
                    getViewType1(positin - 1, convertView, parent);
                    break;
            case VIEW_TYPE_2:
                    getViewType1(positin - 2 - array1.length, convertView, parent);
                    break;
            case VIEW_TYPE_2:
                    getViewType1(positin - 3 - array1.length - array2.length, convertView, parent);
                    break;
            default:
                    return null;
       }
 }

You should then implement the getViewTypeX(int position, View convertView, ViewGoup parent) that should build the view for the object arrayX[position], as usual.

Community
  • 1
  • 1
sonic
  • 1,813
  • 1
  • 16
  • 19
  • This looks simple for a string array.. But when it comes to custom model, finding difficult to implement. Can I set three different adapters for the same listview as I have 3 custom array lists? – Dilipchandar Oct 12 '15 at 12:40
  • I edit my answer to make it more easy to understant (i hope). – sonic Oct 12 '15 at 13:01
0

You can use an ExpandableListView where your data source is a ArrayMap<String, ArrayList<Object>>

Here is an incomplete example of how you could manage your views for your different models.

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        switch (groupPosition) {
            case 1 :
                //TODO: if the layout is different for each group you should check if the convertedView is the one you should be using for this group. 
                //TODO: if it isn't you should inflate, if it is you should reuse
                if (convertView == null || !convertView.getTag() instanceof KittyViewHolder.class) {
                    convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_kitties, null);
                    holder = new KittyViewHolder();
                    holder.name = (TextView) view.findViewById(R.id.kittyname);
                    convertView.setTag(holder);
                }
                else {
                    holder = (KittyViewHolder) view.getTag();
                }

                //You can savely cast since you know your first group contains Kitties (=^・^=)
                Kitty aKitty = (Kitty) getChild(groupPosition, childPosition);
                holder.name.setText(aKitty.getName());
                break;
            case 2:
                //TODO: if the layout is different for each group you should check if the convertedView is the one you should be using for this group. 
                //TODO: if it isn't you should inflate, if it is you should reuse
                if (convertView == null || !convertView.getTag() instanceof SharkHolder.class) {
                    convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_sharks, null);
                    holder = new SharkHolder();
                    holder.thooth = (TextView) view.findViewById(R.id.thoothcount);
                    holder.picture = (ImageView) view.findViewById(R.id.sharkimage);
                    convertView.setTag(holder);
                }
                else {
                    holder = (SharkHolder) view.getTag();
                }

                //You can savely cast since you know your second group contains Sharks
                Shark aShark = (Shark) getChild(groupPosition, childPosition);
                holder.thooth.setText(""+aShark.getToothCount());
                holder.picture.setImageResource(aShark.getBabySharkPicture());
                break;
            case 3:
                //Same thing as above with the correct model type
                break;
        }

        return convertView;
    }
Kalem
  • 1,122
  • 12
  • 31
  • I am using expandable list view.. In that I need to set different adapters for the same listview.. Is that possible? Because all 3 model classes are different. – Dilipchandar Oct 12 '15 at 12:36
  • I don't see why not. If your Model classes share a commun parent or interface your ArrayList would be ArrayList, if not it would be ArrayList. You need to check on `getGroupView` and `getChildView` which object you are dealing with. – Kalem Oct 12 '15 at 12:45
  • If I am setting the same listview to two adapters, it is displaying only one set of adapter. – Dilipchandar Oct 12 '15 at 12:53
  • I edited my answer, hope it is what you are looking for. Your question was put on hold as too broad. You should edit your question and add some code of what you have done until now and/or say the you are using exandable list view. – Kalem Oct 12 '15 at 13:48
  • Thank you so much Kalem.. – Dilipchandar Oct 12 '15 at 19:46
  • I am facing a small problem now.. I am having 3 headers under which three array list is displayed. While clicking one of the list I am getting list size as zero, but data is displayed correctly. Click is working fine and list size also coming correctly at first time. When I come back from next screen and clicking the list again this problem occurs. Can u help me on this? – Dilipchandar Oct 17 '15 at 12:32
  • Please post another question and link me in i'll may ne able to help you. – Kalem Oct 19 '15 at 07:28