Невозможно выбрать RadioButtons при использовании с ExpandableListView - PullRequest
2 голосов
/ 13 ноября 2011

Я пытаюсь создать расширяемый вид списка, подобный описанному здесь .Я использую следующий адаптер для списка:

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

}

1 Ответ

0 голосов
/ 17 апреля 2012
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;
        }
    };
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...