【问题标题】:Only select only one radio button from listview仅从列表视图中选择一个单选按钮
【发布时间】:2013-04-12 03:51:02
【问题描述】:

我一遍又一遍地搜索这个问题,我在stackoverflow中发现了很多问题,但我没有想出解决方案。这是我的问题。 我在这样的线性布局中有一个项目列表:

当屏幕首先出现时,会自动选择最后一个单选按钮。但是如果我选择另一个单选按钮,最后一个单选按钮已经处于选中状态。我只想选择一个单选按钮,但同时当屏幕首先出现时,最后一个单选按钮将被自动选择,而其他单选按钮将不会被选择。 我怎样才能做到这一点。 我的适配器类是:

public class AdapterPaymentMethod extends ArrayAdapter<PaymentData> {
    private Context context;
    private boolean userSelected = false;
    ViewHolder holder;

    public AdapterPaymentMethod(Context context, int resource,
            List<PaymentData> items) {
        super(context, resource, items);
        this.context = context;
    }

    /* private view holder class */
    private class ViewHolder {
        RadioButton radioBtn;
        Button btPaymentMethod;
        // ImageView ivLine;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        holder = null;
        PaymentData rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(
                    com.android.paypal.homeactivity.R.layout.list_item, null);
            holder = new ViewHolder();
            holder.radioBtn = (RadioButton) convertView
                    .findViewById(com.android.paypal.homeactivity.R.id.rdb_payment_method);
            holder.btPaymentMethod = (Button) convertView
                    .findViewById(com.android.paypal.homeactivity.R.id.bt_item_event);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        if(position==getCount()-1 && userSelected==false){
            holder.radioBtn.setChecked(true);
        Log.d("if position", ""+position);
        }
        else{
            holder.radioBtn.setChecked(false);
            Log.d("else position", ""+position);
        }

        holder.radioBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                userSelected=true;
                if(getCount()-1==position&&userSelected==true){         
                }
                else if(getCount()-2==position&&userSelected==true)
                {
                    holder.radioBtn.setChecked(true);
                }
            }
        });



        holder.radioBtn.setText(rowItem.getRdbText());
        holder.btPaymentMethod.setText(rowItem.getBtText());
        return convertView;
    }
}

【问题讨论】:

  • 我也遇到了同样的问题,你能帮我吗,但在复选框中

标签: android listview radio-button


【解决方案1】:

将当前选中的RadioButton 保存在Activity 的变量中。在RadioButtons 的OnClickListener 中执行以下操作:

// checks if we already have a checked radiobutton. If we don't, we can assume that 
// the user clicked the current one to check it, so we can remember it.
if(mCurrentlyCheckedRB == null) 
    mCurrentlyCheckedRB = (RadioButton) v;
    mCurrentlyCheckedRB.setChecked(true);
}

// If the user clicks on a RadioButton that we've already stored as being checked, we 
// don't want to do anything.       
if(mCurrentlyCheckedRB == v)
    return;

// Otherwise, uncheck the currently checked RadioButton, check the newly checked 
// RadioButton, and store it for future reference.
mCurrentlyCheckedRB.setChecked(false);
((RadioButton)v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton)v;   

【讨论】:

  • 您的解决方案工作正常。你能解释一下你的代码吗?我不明白你的代码。我是新手。谢谢。
  • 我添加了一些 cmets 来解释它。如果这对您有用,请将答案设置为已接受!
  • 感谢您的解释。我喜欢它。
  • @catherine 你能解释一下吗,我遇到了同样的问题,但是在复选框中我该如何使用这个条件
  • 你需要解释什么上面没有解释?
【解决方案2】:

这是我的代码:

ListAdapter.java

public class ListAdapter extends BaseAdapter {

    private Context context;
    private String[] version;
    private LayoutInflater inflater;
    private RadioButton selected = null;

    public ListAdapter(Context context, String[] version) {
        this.context = context;
        this.version = version;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return version.length;
    }

    @Override
    public Object getItem(int position) {
        return version;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        final Holder holder;
        if (view == null) {
            holder = new Holder();
            view = inflater.inflate(R.layout.row_list, null);

            holder.radioButton = (RadioButton) view.findViewById(R.id.radio1);
            holder.textViewVersion = (TextView) view.findViewById(R.id.textView1);
            holder.btnMore = (Button) view.findViewById(R.id.button1);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.textViewVersion.setText(version[position]);

        //by default last radio button selected
        if (position == getCount() - 1) {
            if (selected == null) {
                holder.radioButton.setChecked(true);
                selected = holder.radioButton;
            }
        }

        holder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (selected != null) {
                    selected.setChecked(false);
                }
                holder.radioButton.setChecked(true);
                selected = holder.radioButton;
            }
        });       
    }

    private class Holder {
        private RadioButton radioButton;
        private TextView textViewVersion;
        private Button btnMore;
    }
}

希望对你有帮助:)

【讨论】:

  • 谢谢你..你是救生员!! :)
【解决方案3】:

试试这个技巧。

在布局中进行此更改。

<RadioButton
  android:id="@+id/radio"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_vertical"
  **android:clickable="false"
  android:focusable="false"**
/>

在您的适配器中添加以下行:

 private int selectedPosition = -1;

 viewHolder.choiceButton = (RadioButton)convertView.findViewById(R.id.radio);

 if(selectedPosition == -1) {
    viewHolder.choiceButton.setChecked(position==getCount()-1);
 }
 else{
    viewHolder.choiceButton.setChecked(selectedPosition == position);
 }

在 getView() 中指定 selectedPosition:

viewHolder.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPosition = (int) view.getTag();
                notifyDataSetChanged();
            }
        });

【讨论】:

  • 此代码不起作用。多个单选按钮被选中。
  • 您是否使用 getTag() 在点击布局上分配了 selectedPosition ?
【解决方案4】:

在您的 Adapter 类中,获取一个全局变量并如下所示对其进行初始化。

private int lastSelectedPosition = -1;

public class ViewHolder extends RecyclerView.ViewHolder {
    public RadioButton check;
    public ViewHolder(View itemView) {
        super(itemView);
        check=itemView.findViewById(R.id.check_box4);//Your radio button id
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastSelectedPosition = getAdapterPosition();
                notifyDataSetChanged();
            }
        });
    }

public void onBindViewHolder(ViewHolder holder, int position) {
    holder.check.setChecked(lastSelectedPosition == position);
}

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 2018-09-19
    • 1970-01-01
    • 2016-06-17
    • 2014-06-19
    • 1970-01-01
    • 2016-05-15
    • 2012-07-30
    • 1970-01-01
    相关资源
    最近更新 更多