Android 如何在ListView自定义适配器中使用RadioGroup?

Android 如何在ListView自定义适配器中使用RadioGroup?,android,android-layout,radio-button,Android,Android Layout,Radio Button,我想在列表中显示一个选择选项。我正在我的列表视图行中使用单选按钮。我知道RadioGroup用于单个选择 但问题是我在我的列表行视图中添加了单选按钮。现在我想在一个单选按钮中添加所有列表项。我正在使用自定义适配器和getView()。我在getView()中获得了单选按钮,但是当我想在RadioGroup中添加它时,它会说 视图已具有父视图,请在之前在父视图中调用removeView() 我知道这是真的,但如果我把它从视图中移除。那么它就不可见了 我还尝试以编程方式创建和添加RadioButto

我想在列表中显示一个选择选项。我正在我的
列表视图
行中使用
单选按钮
。我知道
RadioGroup
用于单个选择

但问题是我在我的
列表行视图中添加了
单选按钮。现在我想在一个
单选按钮中添加所有列表项。我正在使用
自定义适配器
getView()
。我在
getView()
中获得了
单选按钮
,但是当我想在
RadioGroup
中添加它时,它会说

视图已具有父视图,请在之前在父视图中调用removeView()

我知道这是真的,但如果我把它从视图中移除。那么它就不可见了

我还尝试以编程方式创建和添加
RadioButton
。然后将其添加到
radiotrop
中。然后查看列表行。但这一次,由于父对象是
RadioGroup
,所以它再次表示

视图已具有父视图,请在之前在父视图中调用removeView()

我想做的是一次只选择列表中的一项。我的代码如下

getView
公共类MyAdapter扩展了ArrayAdapter{
私人停车场;
国际资源;
列表mData;
语境;
公共MyAdapter(上下文上下文、int资源、int textViewResourceId、列表数据){
超级(上下文、资源、textViewResourceId、数据);
this.context=上下文;
mData=数据;
mResource=资源;
mInflater=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
convertView=mInflater.flate(mResource,null);
holder=新的ViewHolder();
holder.icon=(ImageView)convertView.findViewById(R.id.icon);
holder.text=(TextView)convertView.findViewById(R.id.text);
holder.comment=(TextView)convertView.findViewById(R.id.comment);
LinearLayout lin=(LinearLayout)convertView.findViewById(R.id.linerList);
RadioButton rbtn=新RadioButton(上下文);
LayoutParams lparam=新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
rbtn.setSelected(假);
holder.check=rbtn;
//radioGroup.addView(rbtn);
lin.addView(rbtn,0);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.text.setText(mData.get(position.getText());
holder.comment.setText(mData.get(position.getComment());
holder.icon.setImageResource(getApplicationContext().getResources().getIdentifier(mData.get(position).getIcon()),
“可绘制”,getPackageName())
) ;
返回视图;
}
}
该行的我的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
    android:id = "@+id/linerList"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip" />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="My Application"
        android:textSize="20sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textColor="@color/white" />
    <TextView
        android:id="@+id/comment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Simple application that shows how to use RelativeLayout"
        android:textSize="14sp"
        android:textColor="@color/light_gray" />
</LinearLayout>


您需要相同的输出或不同的

我的意思是你只能从列表中选择一种语言。是这样吗


符合它。因此,我将给出一个例子,您需要使用CheckedTextView而不是普通的。

我从未使用过它,但AlertDialog将其用于SingleChoice项。因此它肯定会起作用:)

编辑: 别忘了打电话

 listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
你可以放一个

private int selectedIndex = -1;
然后,在getView代码中可以检查

if (position == selectedIndex) {
     rbtn.setSelected ( true );
}
else {
     rbtn.setSelected ( false );
}
并在自定义适配器中添加一个方法:

public void setSelectedIndex(int index) {
    //some range-checks, maybe
    selectedIndex = index;
    //invalidate
}

然后,在您的onItemClickedListener中,您在位置上调用setSelectedIndex。

此解决方案有效且非常干净,但可能有更好的解决方案

您应该使用适配器来管理单选按钮状态

必须保留对上次选中单选按钮的引用,然后单击
RadioButton。单击
可设置上次选中单选按钮
setChecked(false)

还记得将新选择的单选按钮设置为最后选择的单选按钮

见示例:

private class MyAdapter extends ArrayAdapter<String>{

    private int mResourceId = 0;
    private LayoutInflater mLayoutInflater; 
    private RadioButton mSelectedRB;
    private int mSelectedPosition = -1;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
        mResourceId = resource;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;

        if(view == null){

            view = mLayoutInflater.inflate(mResourceId, parent, false);
            holder = new ViewHolder();

            holder.name = (TextView)view.findViewById(R.id.text);
            holder.radioBtn = (RadioButton)view.findViewById(R.id.radioButton1);

            view.setTag(holder);
        }else{
            holder = (ViewHolder)view.getTag();
        }


        holder.radioBtn.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.radioBtn.setChecked(false);
        }else{
            holder.radioBtn.setChecked(true);
            if(mSelectedRB != null && holder.radioBtn != mSelectedRB){
                mSelectedRB = holder.radioBtn;
            }
        }




        holder.name.setText(getItem(position));


        return view;
    }

    private class ViewHolder{
        TextView        name;
        RadioButton     radioBtn;
    }
}
私有类MyAdapter扩展了ArrayAdapter{
私有int mResourceId=0;
私人停车场平面布置;
专用单选按钮mSelectedRB;
private int mSelectedPosition=-1;
公共MyAdapter(上下文上下文、int资源、int textViewResourceId、列表对象){
超级(上下文、资源、textViewResourceId、对象);
mResourceId=资源;
mLayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图=转换视图;
视窗座;
如果(视图==null){
视图=mLayoutFlater.充气(mResourceId,父项,false);
holder=新的ViewHolder();
holder.name=(TextView)view.findViewById(R.id.text);
holder.radioBtn=(RadioButton)view.findViewById(R.id.radioButton1);
视图.设置标签(支架);
}否则{
holder=(ViewHolder)view.getTag();
}
holder.radioBtn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
if(position!=mSelectedPosition&&mSelectedRB!=null){
mSelectedRB.setChecked(false);
}
mSelectedPosition=位置;
mSelectedRB=(单选按钮)v;
}
});
如果(mSelectedPosition!=位置){
保持架无线电基站设置已检查(假);
}否则{
保持架无线电基站设置已检查(正确);
private class MyAdapter extends ArrayAdapter<String>{

    private int mResourceId = 0;
    private LayoutInflater mLayoutInflater; 
    private RadioButton mSelectedRB;
    private int mSelectedPosition = -1;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
        mResourceId = resource;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;

        if(view == null){

            view = mLayoutInflater.inflate(mResourceId, parent, false);
            holder = new ViewHolder();

            holder.name = (TextView)view.findViewById(R.id.text);
            holder.radioBtn = (RadioButton)view.findViewById(R.id.radioButton1);

            view.setTag(holder);
        }else{
            holder = (ViewHolder)view.getTag();
        }


        holder.radioBtn.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.radioBtn.setChecked(false);
        }else{
            holder.radioBtn.setChecked(true);
            if(mSelectedRB != null && holder.radioBtn != mSelectedRB){
                mSelectedRB = holder.radioBtn;
            }
        }




        holder.name.setText(getItem(position));


        return view;
    }

    private class ViewHolder{
        TextView        name;
        RadioButton     radioBtn;
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView 
        android:id="@+id/name"
        android:layout_width="0dp" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        ... />

    <RadioButton
        android:id="@+id/radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        ... />

</LinearLayout>
public class MyRadioAdapter extends BaseAdapter
{
    private Context mContext;
    private ArrayList<Variation> mVariations;
    private int mSelectedVariation;


    public MyRadioAdapter(Context context, ArrayList<Variation> variations, int selectedVariation)
    {
        mContext = context;
        mVariations = variations;
        mSelectedVariation = selectedVariation;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if(view==null) 
        {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.my_radio_adapter_item, null);          
        }

        final Variation variation = mVariations.get(position);

        TextView name = (TextView) view.findViewById(R.id.name);
        RadioButton radio = (RadioButton) view.findViewById(R.id.radio);

        name.setText(variation.getName());
        if(position==mSelectedVariation) radio.setChecked(true);
        else radio.setChecked(false);

        view.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mSelectedVariation = position;
                MyRadioAdapter.this.notifyDataSetChanged();
            }
        });

        return view;
    }

    ...
}
 singleRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checkedButtonPosition = position;
  notifyDataSetChanged();
        }
    });

    if (checkedButtonPosition == position){
        singleRadio.setChecked(true);
    }else {
        singleRadio.setChecked(false);
    }