【问题标题】:Select only one checkbox/radiobutton in the same row and column with Two checkbox/Radio button in a row using Listview Android使用Listview Android在同一行和列中仅选择一个复选框/单选按钮,其中两个复选框/单选按钮在一行中
【发布时间】:2016-03-16 06:38:45
【问题描述】:

我已经使用自定义列表视图,使用适配器类中的适配器类在一个 ROW 中的两个复选框为此,我需要选择一个复选框,无论是 Checkbox1 Row/Column 与 Checkbox 2 相同。 例如,我在 Listview 中有 5 行 Totally 10 Checkbox inside 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);
        }
    }
}

【问题讨论】:

  • 你尝试了什么?
  • 你能发布你的代码吗?
  • 是的,我刚刚在问题下方发布了我的代码
  • 你能帮帮我吗

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


【解决方案1】:

您可以将 group 用于具有两个复选框的单行列表视图的复选框,然后应用以下代码行

 "listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)"

【讨论】:

  • 我怀疑如何为复选框 1 和复选框 2 调用此方法。它仅适用于复选框 1 public View.OnClickListener onStateChangedListener(final CheckBox ry, final int position){ return new View.OnClickListener () { @Override public void onClick(View v) { if (gry.isClickable()) { selectedPosition = position; } 其他 { selectedPosition = -1; } notifyDataSetChanged(); } };
【解决方案2】:

试试下面的代码:

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

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2019-01-21
    相关资源
    最近更新 更多