【问题标题】:Expandable listview with gridview inside on AndroidAndroid上带有gridview的可扩展列表视图
【发布时间】:2015-04-16 12:10:27
【问题描述】:

我正在尝试将一组数据传递给我的 GridAdapter,这样我就可以得到它并作为 gridview 打印,问题是我当前的数组返回 null,我在这里做错了什么?如何获取孩子的数组列表并将其传递给我的 GridAdapter?提前谢谢你。

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;
    private ArrayList<Child> child;

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
    }



    @Override
    public Object getChild(int groupPosition, int childPosition) {
        child = groups.get(groupPosition).getItems();
        return child.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.gridview, null);
        }
        CustomGridView gridView = (CustomGridView) convertView
                .findViewById(R.id.GridView_toolbar);

        gridView.setNumColumns(3);// gridView.setGravity(Gravity.CENTER);//
        gridView.setHorizontalSpacing(5);// SimpleAdapter adapter =
        GridAdapter adapter = new GridAdapter(context, **child**);
        gridView.setAdapter(adapter);// Adapter

        int totalHeight = 0;
        for (int size = 0; size < adapter.getCount(); size++) {
            RelativeLayout relativeLayout = (RelativeLayout) adapter.getView(
                    size, null, gridView);
            TextView textView = (TextView) relativeLayout.getChildAt(0);
            textView.measure(0, 0);
            totalHeight += textView.getMeasuredHeight();
        }
        gridView.SetHeight(totalHeight);
        return convertView;

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

【问题讨论】:

  • 如何在 getChildView 中获取子值列表并传递给 GridAdapter。我在那里看不到任何逻辑。

标签: android android-arrayadapter expandablelistview android-gridview expandablelistadapter


【解决方案1】:

取子数组为

ArrayList<ArrayList<Child>> child = new ArrayList<>();

并在构造函数调用时对其进行初始化

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
    this.context = context;
    this.groups = groups;
    for(int i = 0; i < groups.size(); i++){
        child.add(i, groups.get(i).getItems());
    }
}

并删除

child = groups.get(groupPosition).getItems();

来自 getChild 方法

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多