Getting radio button value from custom list in android(从android中的自定义列表中获取单选按钮值)
问题描述
我在每一行都有一个自定义列表视图和一个单选按钮,它可以正常工作,但我想从此代码中获取所选单选按钮的 ID.例如,当我需要选定行的 textview1 值时,我该如何收集它?提前致谢.这是我的代码:
I have a custom listview and a radio button in each row, it works properly but i want to reach id of selected radio button from this code. For example when i need the textview1 value of selected row, how can i gather it? Thanks in advance. Here is my code:
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;
                }               
            }
推荐答案
我想从此代码中获取所选单选按钮的 ID.
I want to reach id of selected radio button from this code.
你必须玩 setTag &获取 getTag 属性以获取所选单选按钮的实际 id,
You have to play with setTag & get getTag property to fetch actual id of your selected radio button,
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 的准确值了.
This way you will get the exact values of textview from your rows.
这篇关于从android中的自定义列表中获取单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从android中的自定义列表中获取单选按钮值
				
        
 
            
        - 用 Swift 实现 UITextFieldDelegate 2022-01-01
 - 想使用ViewPager,无法识别android.support.*? 2022-01-01
 - 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
 - android 4中的android RadioButton问题 2022-01-01
 - 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
 - Android viewpager检测滑动超出范围 2022-01-01
 - Android - 拆分 Drawable 2022-01-01
 - Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
 - 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
 - MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
 
				
				
				
				