0

我有一个自定义列表视图适配器,其中包含 ArrayList 作为成员。

每场比赛都属于一个回合。我是说:

public class GameSummary
{
 String round;
 Game game;
}

所以实际上,我需要创建一个部分列表视图,该轮将是标题和下面的游戏。

问题是,listview 引擎在数组中生成行 PER 对象。

因此,如果我在其中的数组中有 3 个 GameSummary 和 10 个游戏 - 它只会生成 3 行!

我该怎么办 ?

当前的自定义适配器继承自 BaseAdapter 类。

4

1 回答 1

2

你必须像这样使用 expandableListView 和 customAdapter 。

@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;
        }

和你的班级:

public class GameSummary
{
 String round;
 List<Game> gameList;
}
于 2012-12-01T16:31:30.597 回答