可扩展列表视图getChildrenCount返回null

时间:2015-03-13 14:17:08

标签: android expandablelistadapter

我正在尝试实现一个可扩展列表视图,我已正确设置并显示正确的标题,但内部项目未显示,因为getChildrenCount()始终返回null,这是我的代码:

/**
 * Created by Ahmed on 2/21/2015.
 */
public class CompaniesAdapter extends BaseExpandableListAdapter {

    private ExpandableListView list;
    private Context context;
    private HashMap<Category, ArrayList<Company>> companies;

    public CompaniesAdapter(Context context, HashMap<Category, ArrayList<Company>> companies, ExpandableListView list) {
        this.list = list;
        this.context = context;
        this.companies = companies;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Company> x = companies.get(0);
        Log.v("", "" + x.size());
        return companies.get(groupPosition).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return companies.get(groupPosition).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Set<Category> category = companies.keySet();
        Object[] s =  category.toArray();
        Category cat = (Category) s[groupPosition];
        String title = cat.getTitle();
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.company_header_item, null);
        }

        TextView titleTxt = (TextView) convertView.findViewById(R.id.companiesHeaderItem);
        titleTxt.setText(title);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.companies_list_item, null);
        }

        ImageView companyLogo = (ImageView) convertView.findViewById(R.id.companyLogo);
        Picasso.with(context).load(companies.get(groupPosition).get(childPosition).getImgLink()).into(companyLogo);

        TextView companyName = (TextView) convertView.findViewById(R.id.companyName);
        TextView companyArName = (TextView) convertView.findViewById(R.id.companyArName);

        if (Utils.getDefaultLang(context) == Const.ARABIC) {
            companyName.setText(companies.get(groupPosition).get(childPosition).getArTitle());
            companyArName.setText(companies.get(groupPosition).get(childPosition).getTitle());
        } else {
            companyArName.setText(companies.get(groupPosition).get(childPosition).getArTitle());
            companyName.setText(companies.get(groupPosition).get(childPosition).getTitle());
        }

        return convertView;
    }

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

0 个答案:

没有答案