android自定义单选框文字左边,从Android中的自定义列表获取单选按钮值

作者分享了一段关于在Android或类似应用中如何使用自定义列表视图(ListView)配合单选按钮实现选中项ID获取的代码片段。通过ViewHolder设计模式,了解如何在RadioButton点击事件中保存并访问所选行的TextView1值。
摘要由CSDN通过智能技术生成

我在每行中都有一个自定义列表视图和一个单选按钮,它可以正常工作,但是我想从此代码中访问选定单选按钮的ID.例如,当我需要选定行的textview1值时,如何收集它?提前致谢.这是我的代码:

private static class Adapter extends BaseAdapter {

private LayoutInflater mInflater;

private int mResourceId = 0;

private RadioButton mSelectedRB;

private int mSelectedPosition = -1;

public CitizenAdapter(Context context) {

mInflater = LayoutInflater.from(context);

}

public int getCount() {

return array.size();

}

public Object getItem(int position) {

return position;

}

public long getItemId(int position) {

return position;

}

public View getView(final int position, View convertView, ViewGroup parent) {

final ViewHolder holder;

if (convertView == null) {

convertView = mInflater.inflate(R.layout.table_row_citizen, null);

holder = new ViewHolder();

holder.text1 = (TextView) convertView.findViewById(R.id.TextView01);

holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);

holder.button = (RadioButton)convertView.findViewById(R.id.radioButtonCitizen);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

holder.text1.setText(array1.get(position));

holder.text2.setText(array2.get(position));

holder.button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if((position != mSelectedPosition && mSelectedRB != null)){

mSelectedRB.setChecked(false);

}

mSelectedPosition = position;

mSelectedRB = (RadioButton)v;

}

});

if(mSelectedPosition != position){

holder.button.setChecked(false);

}else{

holder.button.setChecked(true);

if(mSelectedRB != null && holder.button!= mSelectedRB){

mSelectedRB = holder.button;

}

}

return convertView;

}

static class ViewHolder {

TextView text1;

TextView text2;

RadioButton button;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值