在ExpandableListView

时间:2016-10-04 14:59:22

标签: android checkbox expandablelistview

点击一个按钮,我会看到一个Dialog,其布局中有一个ExpandableListView,填充了复选框:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog=new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.selectdialoglayout);
            elv=(ExpandableListView) dialog.findViewById(R.id.elv);
            setParentsItems();
            setChildData();
            MyExpandableAdapter adapter = new MyExpandableAdapter(parents, childs);
            adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), MainActivity.this);
            elv.setAdapter(adapter);
            elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {

                    String componente="";
                    if(i==0){
                        componente=childsIuGeneral.get(i1);
                    }
                    if(i==1){
                        componente=childsIuSuelo.get(i1);
                    }
                    if(i==2){
                        componente=childsIuCon.get(i1);
                    }
                    if(i==3){
                        componente=childsIuComp.get(i1);
                    }
                    if(i==4){
                        componente=childsIuAmort.get(i1);
                    }
                    Log.i("LOG", "Componente seleccionado: "+componente);
                    return true;
                }
            });
            Button close=(Button)dialog.findViewById(R.id.button2);
            close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    });

适配器:

public class MyExpandableAdapter extends BaseExpandableListAdapter {

private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems;
private ArrayList<CheckBox> child;

// constructor
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern)
{
    this.parentItems = parents;
    this.childtems = childern;
}

public void setInflater(LayoutInflater inflater, Activity activity)
{
    this.inflater = inflater;
    this.activity = activity;
}


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

@Override
public int getChildrenCount(int i) {
    return ((ArrayList<CheckBox>) childtems.get(i)).size();
}

@Override
public Object getGroup(int i) {
    return null;
}

@Override
public Object getChild(int i, int i1) {
    return null;
}

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

@Override
public long getChildId(int i, int i1) {
    return 0;
}

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

@Override
public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_group, null);
    }
    TextView textView=(TextView)convertView.findViewById(R.id.textView);
    textView.setText(parentItems.get(i));
    //((TextView) convertView).setChecked(isExpanded);
    return convertView;
}

@Override
public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
    child = (ArrayList<CheckBox>) childtems.get(i);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
    }
    CheckBox cb=(CheckBox)convertView.findViewById(R.id.checkBox);
    cb.setText(child.get(i1).getText());
    cb.setFocusable(false);
    cb.setClickable(false);

    return convertView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}
}

这样,在OnChildClickListener中,我通过访问相应的arraylist获取组件的名称(CheckBox文本中的内容)。但是没有选中CheckBox ...因此用户不知道已经选中了复选框。

如何将复选框设置为已点击,以便用户可以知道他/她的选择已经完成?

1 个答案:

答案 0 :(得分:1)

试试这个:

public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
    /* your code */
    CheckBox cb=(CheckBox)view.findViewById(R.id.checkBox);
    //if you want just set to true
    cb.setChecked(!cb.isChecked());
    return true;
}