English 中文(简体)
Android: 扩展列表视图中的动态标记复选框
原标题:Android: Dynamically marking checkboxes in ExpandableListView

我使用“扩展式ListView”, 儿童行各有一个文本视图, 并在其中复选框 。

我想做的是,当用户按下按钮时, 活动将决定哪些项目被检查, 并对这些项目做一些事情 。

I read many posts on how ExpandableListView and checkboxes currently don t work well, and how I need to keep an additional data structure to keep track of what s has been selected, and not. This post covers that issue: ExpandableListView and checkboxes

完成! 我现在可以追踪所选的和未选的。

现在我想解决没有显示其状态的复选框问题。 我注意到复选框在下列时间随机变化:

  • screen rotation changes
  • a group collapses/opens

我知道,有可能发现这些事件。

我想知道是否有办法手工设置复选框。

ExpandableListView elv = getExpandableListView();
for(int i = 0; i < elv.getChildCount(); i++) {
  CheckBox checkbox = ... somehow get checbox within the list
  checkbox.setChecked(checkList.at(i, j));
}

其中i 和 j 表示哪个组, 和子女, 和 校对列表 记录检查/ 校验的物品 。

然而,我注意到,获得ChildCount () 的价值取决于有多少团体是开放的,而不一定告诉我我可以操纵哪个项目清单。

已经使用了可扩展的ListView, 并且一定有人有办法绕过这个问题。 是否有明智的解决方案? 具体地说, 我正寻找方法检查/ 检查“ 集团扩大” 和“ 组合变化( 或类似事件) ” 列表中的适当项目 。

提前感谢您!

最佳回答

检查以下样本中的可扩展列表适配器

/**
     * A simple adapter which maintains an ArrayList of photo resource Ids. 
     * Each photo is displayed as an image. This adapter supports clearing the
     * list of photos and adding a new photo.
     *
     */
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {


        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" },
                { "Goldy", "Bubbles" }
        };

        private ArrayList<ArrayList<Boolean>> checkboxStatus = new ArrayList<ArrayList<Boolean>>();

        public MyExpandableListAdapter() {
            int groupCount = groups.length;
            for(int i=0; i<groupCount; i++) 
            {
                int childCount = children[i].length;
                ArrayList<Boolean> childStatus = new ArrayList<Boolean>();
                for(int j=0; j<childCount; j++) {
                    childStatus.add(false);
                }
                checkboxStatus.add(childStatus);
            }
        }

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {

            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(ExpandableListCheckboxActivity.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {

            CheckBox chkbox = new CheckBox(ExpandableListCheckboxActivity.this);
            chkbox.setTag("" + groupPosition + "," + childPosition);
            chkbox.setOnCheckedChangeListener(checkboxListener);
            chkbox.setText(children[groupPosition][childPosition]);

            chkbox.setChecked(checkboxStatus.get(groupPosition).get(childPosition));

            return chkbox;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());

            return textView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }

        private OnCheckedChangeListener checkboxListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String positions = buttonView.getTag().toString();
                int groupPosition = Integer.valueOf(positions.split(",")[0]);
                int childPosition = Integer.valueOf(positions.split(",")[1]);
                checkboxStatus.get(groupPosition).set(childPosition, isChecked);
            }
        };

    }
问题回答

如果您只想在单击行时更改它:

elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        CheckBox checkbox = (CheckBox) v.findViewById(r.id.yourcheckbox)
        checkbox.setChecked(checkList.at(groupPosition, childPosition));    
        return true;
    }
});




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签