简体   繁体   English

在Android中创建复选框列表的不同方法

[英]different ways to create checkbox list in android

Could someone please explain the different ways of making checkbox list and saving the checked option? 有人可以解释制作复选框列表和保存选中的选项的不同方法吗? It would be nice if you could attach examples. 如果您可以附加示例,那就太好了。 I am using arraylist to save input, what are the other ways? 我正在使用arraylist保存输入,还有其他方法吗?

final CharSequence[] items = {};
final ArrayList seletedItems = new ArrayList();

You can create a custom class for objects like, 您可以为以下对象创建自定义类,

public class entity
{
  public boolean isChecked;
  //Any other variables can also be created
}

On check box selection change the value of 在复选框选择上更改值

entityobj[index].isChecked=true/false;

Apply a custom list adapter, in adapters getview() method you can check for isChecked value and then either check or uncheck the respective checkbox, using this your selection will persist while scrolling also.. Hope it will help... 应用一个自定义列表适配器,在适配器的getview()方法中,您可以检查isChecked值,然后选中或取消选中相应的复选框,使用它,您的选择还将在滚动时保持不变。希望对您有所帮助...

Define your ListItem object to have a 'checked' field 定义您的ListItem对象以具有“已检查”字段

class ListItem{
boolean isChecked=false;
}

In your list adapter's getView attach a onCheckedChangeListener to the CheckBox and change the checked state of your object. 在列表适配器的getViewonCheckedChangeListener附加到CheckBox并更改对象的选中状态。 something like: 就像是:

final MessageItem Message=getItem(position);
        message.setText(Message.text);
        //set data

        select.setOnCheckedChangeListener(null); //important so that when reusing the view the old listener isn't called
        select.setChecked(Message.selected);
        select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                Message.selected=isChecked;
            }
        }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM