与 ExpandableListView 一起使用时无法选择 RadioButtons

发布于 2024-12-15 04:08:57 字数 3740 浏览 5 评论 0原文

我正在尝试创建一个可扩展列表视图,如此处所述。我正在为列表使用以下适配器:

mAdapter = new SimpleExpandableListAdapter(
                this, groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_list_item_single_choice,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );

我的父母和孩子有相同的孩子,但我需要能够从父母中选择一个孩子,并从另一个父母中选择不同的孩子。 单选按钮出现,但在父列表项中分组。也就是说,您不能从一个父级中选择一个子级,也不能从另一个父级中选择不同的子级。我如何编辑此代码以允许这样做?

我从此处获取此代码:

公共类 myClass 扩展 ExpandableListActivity {

final Context mContext = this;

public static final String GROUP_ID = "Group";
public static final String CHILD_ID = "Child";
public static final int GROUPS = 2;
public static final int CHILDREN = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Set up the adapter for the expandable list */
    setListAdapter(new SimpleExpandableListAdapter(
            mContext,
            getGroups(),
            android.R.layout.simple_expandable_list_item_1,
            new String[] {GROUP_ID}, new int[] {android.R.id.text1},
            getChildren(),
            android.R.layout.simple_list_item_single_choice,
            new String[] {CHILD_ID}, new int[] {android.R.id.text1}));

    /* Get the list, and set the choice mode to multiple - why no WORKY? */
    final ExpandableListView listView = getExpandableListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
    listView.setOnChildClickListener(cl);
}

private OnChildClickListener cl = new OnChildClickListener(){

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        int position = groupPosition * (CHILDREN + 1) + childPosition + 1;
        if (getExpandableListView().isItemChecked(position)) {
            getExpandableListView().setItemChecked(position, false);
        } else {
            getExpandableListView().setItemChecked(position, true);
        }
        return false;
    }
};

/* Generate the names of the groups - not the problem, works fine. */
public List<Map<String, String>> getGroups() {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    Map<String, String> map;
    for (int i = 0; i < GROUPS; ++i) {
        map = new HashMap<String, String>();
        map.put(GROUP_ID, "Group " + i);
        list.add(map);  
    }
    return list;
}

/* Generates the names of the children - not the problem, works fine. */
public List<List<Map<String, String>>> getChildren() {
    List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
    List<Map<String, String>> subList = new ArrayList<Map<String, String>>();
    Map<String, String> map;
    for (int j = 0; j < CHILDREN; ++j) {
        map = new HashMap<String, String>();
        map.put(CHILD_ID, "Child " + j);
        subList.add(map);
    }
    for (int i = 0; i < GROUPS; ++i) {
        list.add(subList);
    }
    return list;
}

}

I am trying to create an expandable list view like the one described here. I am using the following adapter for the list:

mAdapter = new SimpleExpandableListAdapter(
                this, groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_list_item_single_choice,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );

My parent and children have the same children, but I need to be able to select a child from a parent and a different child from another parent.
The radio buttons appear but are grouped across the parent list items. That is, you cannot select a child from on parent and a different child from another parent. How can I edit this code to allow that?

I got this code from here:

public class myClass extends ExpandableListActivity {

final Context mContext = this;

public static final String GROUP_ID = "Group";
public static final String CHILD_ID = "Child";
public static final int GROUPS = 2;
public static final int CHILDREN = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Set up the adapter for the expandable list */
    setListAdapter(new SimpleExpandableListAdapter(
            mContext,
            getGroups(),
            android.R.layout.simple_expandable_list_item_1,
            new String[] {GROUP_ID}, new int[] {android.R.id.text1},
            getChildren(),
            android.R.layout.simple_list_item_single_choice,
            new String[] {CHILD_ID}, new int[] {android.R.id.text1}));

    /* Get the list, and set the choice mode to multiple - why no WORKY? */
    final ExpandableListView listView = getExpandableListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
    listView.setOnChildClickListener(cl);
}

private OnChildClickListener cl = new OnChildClickListener(){

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        int position = groupPosition * (CHILDREN + 1) + childPosition + 1;
        if (getExpandableListView().isItemChecked(position)) {
            getExpandableListView().setItemChecked(position, false);
        } else {
            getExpandableListView().setItemChecked(position, true);
        }
        return false;
    }
};

/* Generate the names of the groups - not the problem, works fine. */
public List<Map<String, String>> getGroups() {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    Map<String, String> map;
    for (int i = 0; i < GROUPS; ++i) {
        map = new HashMap<String, String>();
        map.put(GROUP_ID, "Group " + i);
        list.add(map);  
    }
    return list;
}

/* Generates the names of the children - not the problem, works fine. */
public List<List<Map<String, String>>> getChildren() {
    List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
    List<Map<String, String>> subList = new ArrayList<Map<String, String>>();
    Map<String, String> map;
    for (int j = 0; j < CHILDREN; ++j) {
        map = new HashMap<String, String>();
        map.put(CHILD_ID, "Child " + j);
        subList.add(map);
    }
    for (int i = 0; i < GROUPS; ++i) {
        list.add(subList);
    }
    return list;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-12-22 04:08:57
private OnChildClickListener cl = new OnChildClickListener(){

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

            int position;
            int setposition =childPosition+1;

            if(groupPosition ==0)
            {

                 position = groupPosition * (CHILDREN + 1) + childPosition + 1;
                 if (getExpandableListView().isItemChecked( setposition)) {
                    getExpandableListView().setItemChecked(setposition, false);
                } else {
                    getExpandableListView().setItemChecked( setposition, true);
                }
            }

            else
                {

                 position = count+childPosition + 1;
                 setposition =childPosition+2;

                 if (getExpandableListView().isItemChecked( setposition)) {
                    getExpandableListView().setItemChecked(setposition, false);
                } else {
                    getExpandableListView().setItemChecked( setposition, true);
                }
                }

            System.out.println("is checked? at position:"+ getExpandableListView().isItemChecked(setposition)+position);


            return false;
        }
    };
private OnChildClickListener cl = new OnChildClickListener(){

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

            int position;
            int setposition =childPosition+1;

            if(groupPosition ==0)
            {

                 position = groupPosition * (CHILDREN + 1) + childPosition + 1;
                 if (getExpandableListView().isItemChecked( setposition)) {
                    getExpandableListView().setItemChecked(setposition, false);
                } else {
                    getExpandableListView().setItemChecked( setposition, true);
                }
            }

            else
                {

                 position = count+childPosition + 1;
                 setposition =childPosition+2;

                 if (getExpandableListView().isItemChecked( setposition)) {
                    getExpandableListView().setItemChecked(setposition, false);
                } else {
                    getExpandableListView().setItemChecked( setposition, true);
                }
                }

            System.out.println("is checked? at position:"+ getExpandableListView().isItemChecked(setposition)+position);


            return false;
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文