简体   繁体   中英

How to get string from opened groupView in ExpandableListView?

I have ExpandableListView. It can be only one opened groupView. I want get string from selected GroupView. Im trying get string in getGroupView methods (groupName - var) but it always show text from another groupView. Whats wrong with my code? Help please! Here is my adapter:

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;
    }
}

You should use an holder in you getGroupView. I would recommend you to add an holder for the recycler.

@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        View v = convertView;
        ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
        if (v == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.my_layout, parent, false);
            ViewHolder holder = new ViewHolder();
            holder.textview = v.findViewById(R.id.textGroup));
            holder.group = group;
            v.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) v.getTag();
        // Do whatever you need to with the group view for example:
        // groupName = holder.group.getGroupName();

        return v;
}


public class ViewHolder
{
 TextView textview;
 Group group;
   public ViewHolder()
    {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM