如何从ExpandableListView获取子项

时间:2016-08-27 21:26:50

标签: android expandablelistview

这是ExpandableListView的适配器。我根据我的要求改变了它。我在函数getChild()中面临的问题。我无法生孩子。错误出现在下面的功能中。请帮我解决。

@Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(groupPosition).get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

NavAdapter:

package com.example.zohaibsiddique.inventorycontrol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by root on 11/12/15.
 */
public class NavAdapter extends BaseExpandableListAdapter implements ExpandableListView.OnChildClickListener{

    public LayoutInflater minflater;
    public Activity activity;
    private Context _context;
    private List<String> _listDataHeader; // header titles
    private ArrayList<HashMap<String, Object>> _listDataChild;
    public static TextView txtListChild;

    public NavAdapter(Context context, List<String> listDataHeader,
                      ArrayList<HashMap<String, Object>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(groupPosition).get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

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

        try {
            final String childText = (String) getChild(groupPosition, childPosition);

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

            txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
            txtListChild.setText(childText);

        } catch (Exception e) {
            Log.d("getChildView", " error " + e.getMessage());
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(groupPosition).get(this._listDataHeader.get(groupPosition))
                .size();
    }

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

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

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

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
//        MainActivity.expandableListView.setSelectedGroup(groupPosition);
//        MainActivity.expandableListView.setSelectionFromTop(groupPosition,0);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        ImageView imageView = (ImageView) convertView.findViewById(R.id.group_image);
        imageView.setImageDrawable(convertView.getResources().getDrawable(R.drawable.ic_menu_camera));

//        MainActivity.expandableListView.OnChildClickListener

        return convertView;
    }

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

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

    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
//        Utility.alertDialog(_context, "", String.valueOf());
        return false;
    }
}

0 个答案:

没有答案