android - 从 ListView 中只选择一个单选按钮

标签 android listview radio-button

这个问题我翻了好几遍,在stackoverflow上找到了很多问题,但是我没有想出解决办法。这是我的问题。 我在这样的线性布局中有一个项目列表: enter image description here

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

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

最佳答案

将当前选中的 RadioButton 保存在 Activity 的变量中。在 RadioButtonsOnClickListener 中执行此操作:

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

关于android - 从 ListView 中只选择一个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092991/

相关文章:

javascript - 选择单选按钮后如何根据所选单选按钮显示输出?

javascript - 相关单选按钮和下拉列表

android - 通过 JSON 从 Android 到 ASP.NET 网络服务的用户定义对象列表

listview - 如何在 UWP 中清除虚拟化 ListView 上的选择?

c# - 如何在具有单行ListView 的移动屏幕上快速显示附加数据?

android - 使用 AsyncTask for Android ListView 更新图像缩略图不正确

javascript - 如何手动检查 YUI radio "button"

android - 如何在给定项目处获取 ViewPager 的 subview

android - 可以选择要求 Google Maps API 支持吗?

android - 生成 Google api key 时使用什么包