当我滚动列表视图单选按钮被自动选择或取消选择无法保持单选按钮的状态
Posted
技术标签:
【中文标题】当我滚动列表视图单选按钮被自动选择或取消选择无法保持单选按钮的状态【英文标题】:When I scroll the list view radio button gets automatically selected or deselected not able to maintain the state of radio button 【发布时间】:2020-04-14 16:09:18 【问题描述】:我正在创建一个 android 应用程序,我的应用程序的布局如下所示。
点击提交按钮时,我需要选中的复选框和单选按钮的值。
示例 Linux 未选中,cc(单选按钮)已选中。
记录在列表视图中动态填充,但我无法使其工作。有很多问题。
1) 当我滚动列表视图时,单选按钮被自动选中或取消选中,无法保持单选按钮的状态。 2) 单击按钮时没有得到选中的单选按钮和复选框。
下面是我的布局以及java程序。建议我获取正确的值。
enter image description here
Main.xml
<ListView
android:id="@+id/my_list"
android:layout_
android:layout_ />
<Button
android:id="@+id/submit"
android:layout_
android:layout_
android:text="Submit"
/>
row.xml
<TextView
android:id="@+id/label"
android:layout_
android:layout_
android:text="@+id/label"
android:layout_toRightOf="@+id/check"
android:textSize="20sp" >
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
<RadioGroup
android:id="@+id/radiosex"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/to"
android:layout_
android:layout_
android:layout_marginLeft="10dip"
android:checked="true"
android:text="To" />
<RadioButton
android:id="@+id/cc"
android:layout_
android:layout_
android:layout_marginLeft="10dip"
android:text="CC" />
</RadioGroup>
MyAdapter.java
private class ViewHolder
protected TextView text;
protected CheckBox checkbox;
protected RadioGroup radioGroup;
public class MyAdapter extends ArrayAdapter<Model>
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list)
super(context, R.layout.row, list);
this.context = context;
this.list = list;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder viewHolder = null;
if (convertView == null)
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
viewHolder.checkbox.setTag(position);
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioSex);
viewHolder.radioGroup.setTag(position);
convertView.setTag(viewHolder);
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
viewHolder.checkbox.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
CheckBox checkbox = (CheckBox) v;
int getPosition = (Integer) checkbox.getTag();
list.get(getPosition).setSelected(checkbox.isChecked());
);
viewHolder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
boolean isCcOrIsTo = (checkedId == R.id.cc);
int getPosition = (Integer) group.getTag();
list.get(getPosition).setCcOrIsTo(isCcOrIsTo);
);
return convertView;
Model.java
public class Model
private String name;
private boolean selected;
private boolean isCcOrIsTo;
public Model(String name)
this.name = name;
public String getName()
return name;
public boolean isSelected()
return selected;
public void setSelected(boolean selected)
this.selected = selected;
public boolean isCcOrIsTo()
return isCcOrIsTo;
public void setCcOrIsTo(boolean isCcOrIsTo)
this.isCcOrIsTo = isCcOrIsTo;
@Override
public String toString()
String selectedString = selected ? "selected" : "not selected";
String value = isCcOrIsTo ? "CC" : "To";
return name+" -> "+selectedString+ " with value "+value;
MainActivity.java
public void onCreate(Bundle icicle)
super.onCreate(icicle);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.my_list);
btn = (Button) findViewById(R.id.submit);
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
for (Model m : list)
Log.i("Stack1", m.toString());
);
ArrayAdapter<Model> adapter = new MyAdapter(this, getModel());
listView.setAdapter(adapter);
【问题讨论】:
您需要手动设置选中的单选按钮,如复选框。 [这里][1] 是如何检查单选按钮的链接。 [1]:developer.android.com/reference/android/widget/… 【参考方案1】:你没有得到正确的单选和复选框值。 对于单选按钮执行此操作,它会自动选择单选组下的单选 ID 及其文本:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@SuppressLint("ResourceType")
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
String text= radioButton.getText().toString();
Toast.makeText(YourActivity.this, text, Toast.LENGTH_SHORT).show();
);
对于检查框:
checkBox.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (checkBox.isChecked())
String text= checkBox.getText().toString();
);
【讨论】:
以上是关于当我滚动列表视图单选按钮被自动选择或取消选择无法保持单选按钮的状态的主要内容,如果未能解决你的问题,请参考以下文章