仅从列表视图中选择一个单选按钮 [英] Only select only one radio button from listview

查看:109
本文介绍了仅从列表视图中选择一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经反复搜索了这个问题,在stackoverflow中发现了很多问题,但是我没有提出解决方案.这是我的问题. 我有一个线性布局中的项目列表,如下所示:

I have searched the question over and over, there are many questions I have found in stackoverflow but I did not come up with a solution. Here is my problem. I have a list of item in a linearlayout like this:

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

When the screen comes first, the last radion button is selected automatically. But if I select another radio button, the last radio button is already in selected state. I want to select only one radion button but at the same time when the screen comes first the last radio button will be auto selected and other automatically will not be selected. How can I achieve this. My adapter class is :

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中执行以下操作:

Keep the currently checked RadioButton in a variable in your Activity. In the OnClickListener of your RadioButtons do this:

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

这篇关于仅从列表视图中选择一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆