【问题标题】:Using radio button in custom listview in Android在 Android 的自定义列表视图中使用单选按钮
【发布时间】:2013-01-14 12:47:31
【问题描述】:

我有一个使用 getView 完成的自定义列表视图。它必须是每行右侧的 4 个文本视图和一个单选按钮。我在这里看到了很多关于此的示例,但无法理解。这是我的 getView 函数。在这种情况下,我可以看到行,但是当我选择另一个单选按钮时,单选按钮仍然被选中。我应该在函数中添加什么才能使单选按钮正常工作并获取所选单选按钮的 ID?谢谢

private static class CitizenAdapter extends BaseAdapter {
         private LayoutInflater mInflater;
         public CitizenAdapter(Context context) {
                   mInflater = LayoutInflater.from(context);
                    }
         public int getCount() {
                    return sicilArr.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.text3 = (TextView) convertView.findViewById(R.id.TextView03);
                        holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);
                        holder.radioCitizen = (RadioGroup) convertView.findViewById(R.id.radioGroupCitizen);
                        holder.radioButCitizen  = (RadioButton) convertView.findViewById(R.id.radioButtonCitizen);

                        holder.radioCitizen.setOnCheckedChangeListener(new OnCheckedChangeListener() {                              
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {

                            }
                        });
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }       
                   holder.text1.setText(array1.get(position));
                   holder.text2.setText(array2.get(position));
                   holder.text3.setText(array3.get(position));  
                   holder.text4.setText(array4.get(position));


            return convertView;
           }        
             static class ViewHolder {      
                  TextView text1;
                  TextView text2;
                  TextView text3;
                  TextView text4;

                  RadioGroup radioCitizen;
                  RadioButton radioButCitizen;
                }       
            }       

这也是我的 xml 行文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:gravity="left|center"
android:layout_width="wrap_content"
android:paddingBottom="10px"
android:paddingTop="10px"
android:paddingLeft="3px">    
 <TextView
    android:id="@+id/TextView01"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="15dip"
    android:textStyle="bold"
    android:textColor="#d08021">
 </TextView>
<TextView
    android:id="@+id/TextView02"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="15dip"
    android:textStyle="bold"
    android:textColor="#81A2BC">
 </TextView>
 <TextView
    android:id="@+id/TextView03"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="15dip"
    android:textStyle="bold"
    android:textColor="#81A2BC">
 </TextView>    
 <TextView
    android:id="@+id/TextView04"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="15dip"
    android:textStyle="bold"
    android:textColor="#81A2BC">
 </TextView> 
 <RadioGroup 
    android:id="@+id/radioGroupCitizen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RadioButton
                        android:id="@+id/radioButtonCitizen"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
 </RadioGroup>

【问题讨论】:

    标签: android android-listview radio-button


    【解决方案1】:

    一种方法是手动取消选中所有其他单选按钮。您可以创建变量来保持当前选定项目的位置。在getView 中,您可以根据当前位置是否等于选定的位置来选中/取消选中当前单选按钮。使用单选按钮时,您可以更改所选项目的位置并致电notifyDatasetChanged
    还要检查这个问题:Android ListView with RadioButton in singleChoice mode and a custom row layout

    1) 将private int selected = -1 添加到CitizenAdapter
    2)为每个单选按钮添加点击监听器:

    holder.radioCitizen.setOnClickLisener(new View.OnClickListener() 
        {                              
            @Override
            public void onCheckedChanged(View view) {
                selected = (Integer) view.getTag();
                notifyDataSetChanged();
            }
        });
    

    3) 将每个单选按钮的标签设置为当前位置并设置选中状态:

    // After you set text to 4 textviews
    holder.radioButCitizen.setTag(position);
    holder.radioButCitizen.setChecked(position == selected);
    

    【讨论】:

    • 在该链接上接受的答案中,再次有这样的解释,您可以指定一些代码吗?谢谢
    • 好的,在这里找到它stackoverflow.com/questions/7329856/…。不过还是感谢您的关注。
    • 谢谢,这是最好的解决方案。您的答案应标记为CHECKED。 :)
    【解决方案2】:
        int selected = 0;
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        // TODO Auto-generated method stub
    
        ViewHolder holder;
        final RadioButton motif;
    
        if (null == convertView) 
        {
            convertView = View
                    .inflate(parent.getContext(), R.layout.motif_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
    
        holder = (ViewHolder) convertView.getTag();
        motif = (RadioButton) holder.getmotif();
    
        motif.setTag(position);
    
        motif.setText(motifList.get(position));
    
        if(position == 0)
        {
            motif.setChecked(true);
        }
        else
            motif.setChecked(false);
    
        motif.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                selected = position;
                notifyDataSetChanged();
            }
        });
    
        if(position == selected)
            motif.setChecked(true);
        else
            motif.setChecked(false);
    
        return convertView;
    }
    
    private class ViewHolder {
    
        private View mRow;
        private RadioButton motif = null;
    
        public ViewHolder(View row) {
            mRow = row;
        }
    
        public RadioButton getmotif() {
            if (motif == null) {
                motif = (RadioButton) mRow.findViewById(R.id.itemMotif);
            }
            return motif;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      相关资源
      最近更新 更多