如何从ExpandableListView中打开的groupView获取字符串?

时间:2016-04-16 14:25:02

标签: android expandablelistview

我有ExpandableListView。它只能是一个打开的groupView。我想从选定的GroupView中获取字符串。我尝试在getGroupView方法(groupName - var)中获取字符串,但它始终显示来自另一个groupView的文本。我的代码有什么问题?请帮忙!这是我的适配器:

public class ExpandListAdapter extends BaseExpandableListAdapter {    
    private Context context;
    private ArrayList<ExpandListGroup> groups;
    public ArrayList<String> selectedStrings = new ArrayList<String>();
    public String groupName;
    final String LOG_TAG = "myLogs";
    public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) {
        this.context = context;
        this.groups = groups;
    }

    public void addItem(ExpandListChild item, ExpandListGroup group) {
        if (!groups.contains(group)) {
            groups.add(group);
        }
        int index = groups.indexOf(group);
        ArrayList<ExpandListChild> ch = groups.get(index).getItems();
        ch.add(item);
        groups.get(index).setItems(ch);
    }
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
        return chList.get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
        if (view == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.child_view, null);
        }
        // TextView tv = (TextView) view.findViewById(R.id.textChild);
        final CheckBox cb = (CheckBox) view.findViewById(R.id.textChild);
        cb.setText(child.getName().toString());
        cb.setTag(child.getTag());
        cb.setChecked(ExpandListChild.isSelected);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedStrings.add(cb.getText().toString());
                }else{
                    selectedStrings.remove(cb.getText().toString());
                }
            }
        });

        return view;
    }

    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();

        return chList.size();
    }

    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
        ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
        if (view == null) {
            LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inf.inflate(R.layout.group_view, null);
        }
        TextView tv = (TextView) view.findViewById(R.id.textGroup);
        tv.setText(group.getName());

//-------------------------------------------------------------------
//In this place, Im tryimg to get string from opened GroupView
            groupName = group.getName();
//-------------------------------------------------------------------
//-------------------------------------------------------------------------
        // TODO Auto-generated method stub
        selectedStrings.clear();

        return view;
    }

    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

您应该在getGroupView中使用持有者。我建议你为回收商添加一个支架。

{{1}}