gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:45 24 4
gpt4 key购买 nike

这个问题我翻了好几遍,在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com