使用Listview Android,在同一行和列中仅使用两个复选框/单选按钮选择一个复选框/单选按钮

时间:2016-03-16 06:38:45

标签: android android-layout listview android-fragments android-intent

我在Adapter类中使用了适配器类的Customized Listview在ROW中的两个复选框为此我需要选中一个复选框Checkbox1 Row / Column与Checkbox 2相同。 例如,我在Listview中有5行共有10个Checkbox里面的Listview Out 10只有一个选择时间所有其他应该取消选择

任何想法关于我尝试过Lot但仅选择Checkbox1列 请帮忙

public class ListViewAdapter extends ArrayAdapter<Friend> {

    private List<Friend> myFriends;
    private Activity activity;
    private int selectedPosition = -1;
    String flag="";

    String[]ids=new String[100];
    String[]ids1=new String[100];

    public ListViewAdapter(Activity context, int resource, List<Friend> objects) {
        super(context, resource, objects);
        this.activity = context;
        this.myFriends = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final 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);  // This line is important.
        holder.checkBox1.setTag(position + 100);
        holder.friendName.setText(getItem(position).getName());
        holder.rdgrp.setTag(position);

        ids[position]=holder.checkBox.getTag().toString();
        ids1[position]=holder.rdgrp.getTag().toString();
        // System.out.println("praveen"+ids[position]);
        if (position == selectedPosition) {
            holder.rdgrp.setActivated(true);
        } else holder.rdgrp.setActivated(false);

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

        return convertView;
    }

    public View.OnClickListener onStateChangedListener(final RadioGroup gry, final int position ){
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gry.isClickable()) {
                    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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用组的列表视图的单行复选框,其中有两个复选框,然后应用以下代码行

 "listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)"

答案 1 :(得分:0)

尝试使用以下代码:

public class CustomAdapter extends BaseAdapter{
    int selectedPosition = -1; // row position
    int selectedCheckbox = -1; // 1 for 1st, 2 for 2nd on each row

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position == selectedPosition){
            if(selectedCheckbox == 1){
                checkbox1.setChecked(true);
                checkbox2.setChecked(false);
            } else if(selectedCheckbox == 2){
                chkbox1.setChecked(false);
                chkbox2.setChecked(true);
            }
        } else{
            checkbox1.setChecked(false);
            checkbox2.setChecked(false);
        }

        checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    selectedPosition = position;
                    selectedCheckbox = 1;
                } else{
                    selectedPosition = -1;
                    selectedCheckbox = -1;
                }
                notifyDataSetChanged();
            }
        });

        checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    selectedPosition = position;
                    selectedCheckbox = 2;
                } else{
                    selectedPosition = -1;
                    selectedCheckbox = -1;
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }
}

希望,它会帮助你!