如何在列表视图中只选择一个复选框,在android中有多个复选框?

时间:2016-03-14 22:53:30

标签: android android-layout listview

我在listview中的两个复选框中使用第一个colunm复选框工作第二列checkbox1在列表视图中无法一次只选择一个

点击here查看截图

    i tried this code please help me 

我试过这段代码请帮帮我

public class ListViewAdapter extends ArrayAdapter<Friend> {
        private List<Friend> myFriends;
        private Activity activity;
        private int selectedPosition = -1;

        public ListViewAdapter(Activity context, int resource, List<Friend> objects) {
            super(context, resource, objects);

            this.activity = context;
            this.myFriends = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            // If holder not exist then locate all view from UI file.
            if (convertView == null) {
                // inflate UI from XML file
                convertView = inflater.inflate(R.layout.item_listview, parent, false);
                // get all UI view
                holder = new ViewHolder(convertView);
                // set tag for holder
                convertView.setTag(holder);
            } else {
                // if holder created, get tag from view
                holder = (ViewHolder) convertView.getTag();
            }

            holder.checkBox.setTag(position); 
             holder.checkBox1.setTag(position+1); 

            holder.friendName.setText(getItem(position).getName());


            if (position == selectedPosition) {
                holder.checkBox.setChecked(true);
            } else holder.checkBox.setChecked(false);

            if(holder.checkBox.getTag().equals(position){
    holder.checkBox.setOnClickListener(onStateChangedListener(holder.checkBox, position));
    }else{
    holder.checkBox1.setOnClickListener(onStateChangedListener(holder.checkBox1, position));
    }

            return convertView;
        }

        private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) {
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checkBox.isChecked()) {
                        selectedPosition = position;
                    } else {
                        selectedPosition = -1;
                    }
                    notifyDataSetChanged();
                }
            };
        }

        private static class ViewHolder {
            private TextView friendName;
            private CheckBox checkBox,checkBox1;

            public ViewHolder(View v) {
                checkBox = (CheckBox) v.findViewById(R.id.check);
                checkBox1 = (CheckBox) v.findViewById(R.id.check1);
                friendName = (TextView) v.findViewById(R.id.name);
            }
        }
每次第一列复选框工作正常时,

也无法调用此onStateChangedListener方法,否则部分不工作控件不会到这里holder.checkBox1.getTag()holder.checkBox1.setOnClickListener(onStateChangedListener(holder.checkBox1,位置));

In the hole list view only one checkbox should select how will achive this any one please help me 

1 个答案:

答案 0 :(得分:0)

使复选框的标记成为一个字符串,其中包含列表中的位置和框的ID 我经常使用function showForm($atts = array()) { $keywords = filter_input(INPUT_GET, 'sas-keywords'); $form = '<form action="' . get_permalink($atts['results-page']) . '"name="sas-form" class="sas-form form-inline pull-xs-right"><input type="hidden" name="sas-search" value="1"><input type="text" class="form-control" name="sas-keywords" value="' . htmlentities($keywords) . '"/>' . '&nbsp;<input type="submit" value="Search" class="btn btn-secondary" /></form>'; return $form; }

示例:

string.split()

然后,在获取标签时:

holder.checkbox.setTag(checkboxid + ";" + position);

,其中

  • tag [0] =复选框的ID
  • tag [1] =列表中的位置