gpt4 book ai didi

android - 如何在 adapterView 中生成带有单选组的动态单选按钮?

转载 作者:太空狗 更新时间:2023-10-29 16:23:38 26 4
gpt4 key购买 nike

我有一个动态列表,我想在带有单选按钮的适配器 View 中显示该列表。在一组。

我用过:

public class Type2Adapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
ArrayList<QueOption> oplist;
String list1;
int count;
RadioGroup radiogroup;
String dot,more;
public Type2Adapter(Context context,ArrayList<QueOption> arrayList){
this.context = context;
inflater = LayoutInflater.from(context);
oplist = arrayList;
count = oplist.size();
if(count>=1){
count = oplist.size();
}else{
count = 1;
}
}


@Override
public int getCount() {
return count;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

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

ViewHolder holder;
if(convertView == null){

holder = new ViewHolder();
convertView = inflater.inflate(R.layout.type2_option, null);
// holder.nameList = (RadioButton)convertView.findViewById(R.id.optionRadio);
holder.layout = (LinearLayout)convertView.findViewById(R.id.type2);
radiogroup = (RadioGroup)convertView.findViewById(R.id.optionRadioGroup);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.layout.setBackgroundResource(R.drawable.list_back_rowcolor);
// holder.nameList.setText(oplist.get(position).getOptionName());
RadioButton newRadioButton = new RadioButton(context);
newRadioButton.setId(position);
newRadioButton.setText(oplist.get(position).getOptionName());
radiogroup.addView(newRadioButton);

return convertView;
}

public class ViewHolder{

private LinearLayout layout;
private RadioButton nameList;
}

}

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="fill_parent">

<LinearLayout
android:id="@+id/type2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<RadioGroup
android:id="@+id/optionRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >


</RadioGroup>

</LinearLayout>

</LinearLayout>

我应该怎么做才能解决我的问题。

最佳答案

  <RadioGroup 
android:id="@+id/optionRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RadioGroup>

//here i am adding 6 radiobutton .

rgroup=(RadioGroup)findViewById(R.id.optionRadioGroup);

for(int i=0; i<6; i++)
{
rb[i]=new RadioButton(context);
rb[i].setText("rdo"+i);
rb[i].setId(i);
rgroup.addView(rb[i]);
}

关于android - 如何在 adapterView 中生成带有单选组的动态单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com