【问题标题】:Unable to get value from child in expandable List view无法从可展开列表视图中的子项获取价值
【发布时间】:2013-10-11 04:01:08
【问题描述】:

无法从可展开列表视图中的子项获取值。我有两个 列表中的参数..

私有地图> ExpCollection;私人的 列出笔记本电脑;

并试图从价格 TextView 中的 getChildView() 和 setText() 中的 ExpCollection 中获取价值。它抛出空指针异常..

请帮忙...

适配器代码:-

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity context;
    private Map<String, List<String>> ExpCollection;
    private List<String> laptops;


    public ExpandableListAdapter(Activity context, List<String> laptops,
            Map<String, List<String>> ExpCollection) {
        this.context = context;
        this.ExpCollection = ExpCollection;
        this.laptops = laptops;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return ExpCollection.get(laptops.get(groupPosition)).get(childPosition);
    }

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

    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String laptop = (String) getChild(groupPosition, childPosition);

        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_item, null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.laptop);
        TextView price = (TextView) convertView.findViewById(R.id.runTime1);
        TextView discountPrice = (TextView) convertView.findViewById(R.id.runTime);
        System.out.println("ExpCollection******** "+ExpCollection.toString());
    //  price.setText(ExpCollection.get("price").toString());
        //discountPrice.setText(ExpCollection.get("discountPrice").toString());

        RelativeLayout delete = (RelativeLayout) convertView
                .findViewById(R.id.layout);

        item.setText(laptop);
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return ExpCollection.get(laptops.get(groupPosition)).size();
    }

    public Object getGroup(int groupPosition) {
        return laptops.get(groupPosition);
    }

    public int getGroupCount() {
        return laptops.size();
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String laptopName = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_item, null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.laptop);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(laptopName);
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

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

【问题讨论】:

  • 请发布您的logcat,然后只有一个可以提供您的问题的解决方案。
  • 您是否尝试过调试代码并检查laptop 的值是否为空?

标签: android android-listview adapter android-adapter


【解决方案1】:

使用以下内容:

        if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);

【讨论】:

    猜你喜欢
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多