自定义列表视图适配器中的对象数组

时间:2012-12-01 15:45:01

标签: android android-layout android-listview

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

每个游戏都属于一个回合。 我的意思是:

public class GameSummary
{
 String round;
 Game game;
}

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

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

所以,如果我在阵列中有3个GameSummary和10个游戏 - 它只会产生3行!

我该怎么办?

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

1 个答案:

答案 0 :(得分: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;
}