0

I have a custom listview adapter which contains ArrayList as a member.

Each game belongs to a round. I mean:

public class GameSummary
{
 String round;
 Game game;
}

So actually, I need to create sort of a section list view, which the round will be the header and the games below.

The problem is, that the listview engine generates row PER object in the array.

So if I will have 3 GameSummary and 10 Games in the array in it - It will generate only 3 rows!

What should I do ?

The current custom adapter inherites from BaseAdapter class.

1
  • 1
    With baseAdapter, the number of rows is controlled by the getCount method. But you look you'd be benefitting from an expandableListView instead. Commented Dec 1, 2012 at 15:50

1 Answer 1

2

you have to use expandableListView and customAdapter like this.

@SuppressLint("ResourceAsColor")
    public class ExpandableListAdapter extends BaseExpandableListAdapter {


        private LayoutInflater inflater;
        private ArrayList<GameSummary> mParent;

    public  ExpandableListAdapter(Context context, ArrayList<GameSummary> parent){
            this.mParent = parent;
            this.inflater = LayoutInflater.from(context);
        }


        //counts the number of group/parent items so the list knows how many times calls getGroupView() method
        public int getGroupCount() {
            return mParent.size();
        }

        //counts the number of children items so the list knows how many times calls getChildView() method
        public int getChildrenCount(int i) {
            return mParent.getGameList(i).size();
        }

        //gets the title of each parent/group
        public Object getGroup(int i) {
            return mParent.getGameList(i).getTitle(); //game Title
        }

        //gets the name of each item
        public Object getChild(int i, int i1) {
            return mParent.getGameList(i);
        }

        public long getGroupId(int i) {
            return i;
        }

        public long getChildId(int i, int i1) {
            return i1;
        }

        public boolean hasStableIds() {
            return true;
        }


        //in this method you must set the text to see the parent/group on the list
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

            if (view == null) {
                view = inflater.inflate(R.layout.expandable_listview, viewGroup,false);
            }

            TextView textView = (TextView) view.findViewById(R.id.list_item_text_parent);
            //"i" is the position of the parent/group in the list
            textView.setText(getGroup(i).toString());


            //return the entire view
            return view;
        }


        //in this method you must set the text to see the children on the list
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = inflater.inflate(R.layout.expandable_listview_child_item, viewGroup,false);
            }

            TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
            //"i" is the position of the parent/group in the list and 
            //"i1" is the position of the child
            textView.setText(mParent.get(i).getGameList(i1));

            //return the entire view
            return view;
        }

        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

and your class:

public class GameSummary
{
 String round;
 List<Game> gameList;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.