复选框已选中可扩展列表子中的更改

时间:2012-03-21 12:38:23

标签: java android checkbox expandablelistview oncheckedchanged

如何在每个childelements复选框上设置OnCheckedChangeListener? 我无法弄清楚。当我选中一个复选框时,我想要一个Toast弹出,告诉我我检查了哪个孩子:)

来源:

public class ExpandableActivity extends ExpandableListActivity {

private String[] alphabet = { "a", "b", "c", "d", "e", "f", "g" };
private String c;

ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();

private Context context;

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    setContentView(R.layout.expandablelist);

    Bundle extras = getIntent().getExtras();

    c = extras.getString("category");

    SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
            this, createGroupList(), // Creating group List.
            R.layout.group_row, // Group item layout XML.
            new String[] { "Group Item" }, // the key of group item.
            new int[] { R.id.row_name }, // ID of each group item.-Data
                                            // under the key goes into
                                            // this TextView.
            createChildList(), // childData describes second-level
                                // entries.
            R.layout.child_row, // Layout for sub-level entries(second
                                // level).
            new String[] { "Sub Item" }, // Keys in childData maps to
                                            // display.
            new int[] { R.id.grp_child } // Data under the keys above go
                                            // into these TextViews.
    );
    setListAdapter(expListAdapter); // setting the adapter in the list.

}


@SuppressWarnings({ "rawtypes", "unchecked" })
private List createGroupList() {
    ArrayList result = new ArrayList();
    for (String s : alphabet) { // 15 groups........

        HashMap m = new HashMap();

        if (getResources().getIdentifier(c + s + "_name", "string",
                "dk.android.houseenabler") != 0) {
            int id = getResources().getIdentifier(c + s + "_name",
                    "string", "dk.android.houseenabler");
            getResources().getText(id);
            m.put("Group Item", getResources().getText(id)); // the key and
                                                                // it's
                                                                // value.

        } else {
            return result;
        }
        result.add(m);
    }
    return result;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private List createChildList() {

    ArrayList<ArrayList> result = new ArrayList<ArrayList>();
    for (String s : alphabet) {
        if (getResources().getIdentifier(c + s + "_name", "string",
                "dk.android.houseenabler") != 0) {
            int id = getResources().getIdentifier(c + s, "array",
                    "dk.android.houseenabler");

            String[] Rchilds = getResources().getStringArray(id);
            ArrayList<HashMap> secList = new ArrayList<HashMap>();
            for (String d : Rchilds) {
                HashMap childs = new HashMap();
                childs.put("Sub Item", d);
                secList.add(childs);
            }
            result.add(secList);
        } else {
            return result;
        }

    }

    return result;

}

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    // GET TITLE
    int idTitle = getResources().getIdentifier(c + alphabet[groupPosition],
            "array", "dk.android.houseenabler");

    String[] Rchilds = getResources().getStringArray(idTitle);

    // GET Desctiption

    int idDesc = getResources().getIdentifier(
            c + alphabet[groupPosition] + "_desc", "array",
            "dk.android.houseenabler");

    String[] RDesc = getResources().getStringArray(idDesc);

    Intent intent2 = new Intent().setClass(this, Description.class);
    intent2.putExtra("title", Rchilds[childPosition]);
    intent2.putExtra("description", RDesc[childPosition]);
    startActivity(intent2);

    return true;
}

/* This function is called on expansion of the group */
public void onGroupExpand(int groupPosition, View v, ViewGroup vg) {

}
}

这是正在运行的应用程序的图像。 http://www.thomasbolander.dk/app.jpg

2 个答案:

答案 0 :(得分:2)

我找到了解决方案!

    SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
            this, createGroupList(), // Creating group List.
            R.layout.group_row, // Group item layout XML.
            new String[] { "Group Item" }, // the key of group item.
            new int[] { R.id.row_name }, // ID of each group item.-Data
                                            // under the key goes into
                                            // this TextView.
            createChildList(), // childData describes second-level
                                // entries.
            R.layout.child_row, // Layout for sub-level entries(second
                                // level).
            new String[] { "Sub Item" }, // Keys in childData maps to
                                            // display.
            new int[] { R.id.grp_child } // Data under the keys above go
                                            // into these TextViews.
    ) {
        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            View v = super.getChildView(groupPosition, childPosition,
                    isLastChild, convertView, parent);

            if (v != null) {
                final String value = "Check Changed for Checkbox in tab: "+ c + ", on Group: " + groupPosition
                        + ", on Child: " + childPosition;
                CheckBox cb = (CheckBox) v.findViewById(R.id.check);
                cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton arg0,
                            boolean arg1) {
                        Toast toast = Toast.makeText(ExpandableActivity.this, value, 2000);
                        toast.show();
                    }
                });
            }

            return v;
        }
    };
    setListAdapter(expListAdapter); // setting the adapter in the list.

答案 1 :(得分:0)

您可以在ExpandableListView上使用添加setOnChildClickListener(ExpandableListView.OnChildClickListener onChildClickListener)的方法。 OnChildClickListener接口将为您提供一个知道孩子被点击的钩子,然后您可以根据需要更改复选框的状态并转到下一个深度级别。