我在每一行都有一个自定义列表视图和一个单选按钮,它工作正常,但我希望从此代码中获取所选单选按钮的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)
假设您需要访问TextView
方法中的onClick
@Override
public void onClick(View v) {
if((position != mSelectedPosition && mSelectedRB != null)){
mSelectedRB.setChecked(false);
}
mSelectedPosition = position;
mSelectedRB = (RadioButton)v;
View containerView = (View)v.getParent();
TextView textView1 = (TextView)containerView.findViewById(R.id.TextView01);
}
编辑:
我发现您已将holder
对象声明为final
。在这种情况下,您可以直接从事件侦听器中访问holder.text1
和holder.text2
对象。
答案 1 :(得分:0)
我想从此代码中获取所选单选按钮的ID。
您必须使用setTag & get getTag
属性来获取所选单选按钮的实际id
,
holder.button.setTag(position);
holder.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit @ position : " + pos);
String _Str1=array1.get(pos);
String _Str2=array2.get(pos);
}
});
这样您就可以从行中获得textview的确切值。
答案 2 :(得分:0)
按钮onClick
中的代码如下 int selected_radioISAwayGroup = holder.radioISAwayGroup.getCheckedRadioButtonId();
holder.radioISAwayButton = (RadioButton) findViewById(selected_radioISAwayGroup);
System.out.println("holder.radioISAwayButton:"+holder.radioISAwayButton.getText().toString());
if(holder.radioISAwayButton.getText().toString().equalsIgnoreCase("Pre"))
{
//Count +1 for presents
}
else if(holder.radioISAwayButton.getText().toString().equalsIgnoreCase("Abs"))
{
//Count +1 for Absents
}
else
{
//Count +1 for Half
}