gpt4 book ai didi

java - 未维护 RadioGroup 状态的自定义 ListView View Holder 模式

转载 作者:行者123 更新时间:2023-12-01 10:35:19 25 4
gpt4 key购买 nike

我已经使用 RadioGroup 实现了 CustomListView。当我选择第一行/开始行的单选按钮并滚动到列表末尾时,会自动选择最后一行元素。当我滚动回到列表的开头时,Radiogroup 状态不会得到维护。我在这个问题上花了很多时间。请帮助我解决我做错的地方。

MyAdapter.java

public class MyAdapter extends ArrayAdapter<Model> {

private final List<Model> list;
private final Activity context;

public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}

static class ViewHolder {
protected TextView text;
protected RadioGroup radioGroup;
protected RadioButton firstRadio;
protected RadioButton secondRadio;
}

@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.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group);
viewHolder.firstRadio = (RadioButton) convertView.findViewById(R.id.first_rad);
viewHolder.secondRadio = (RadioButton) convertView.findViewById(R.id.second_rad);
viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int selectedID) {
int getPosition = (Integer) radioGroup.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(selectedID); // Set the value of checkbox to maintain its state.
}
});

convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.radio_group, viewHolder.radioGroup);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.radioGroup.setTag(position); // This line is important.

viewHolder.text.setText(list.get(position).getName());
if(list.get(position).getSelected() == viewHolder.firstRadio.getId()){
viewHolder.firstRadio.setChecked(true);
}else if(list.get(position).getSelected() == viewHolder.secondRadio.getId()){
viewHolder.secondRadio.setChecked(true);
}

return convertView;
}
}

行.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30sp" >
</TextView>

<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/label"
android:orientation="horizontal">

<RadioButton
android:id="@+id/first_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</RadioButton>

<RadioButton
android:id="@+id/second_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</RadioButton>
</RadioGroup>

模型.java

public class Model {

private String name;
private int selected;

public Model(String name) {
this.name = name;
}

public String getName() {
return name;
}

public int getSelected() {
return selected;
}

public void setSelected(int selected) {
this.selected = selected;
}
}

最佳答案

如下更改if语句中的条件,它将起作用。

if(list.get(position).getSelected() == viewHolder.firstRadio.getId())
{
viewHolder.firstRadio.setChecked(true);
viewHolder.secondRadio.setChecked(false);
}
else if(list.get(position).getSelected() == viewHolder.secondRadio.getId())
{
viewHolder.secondRadio.setChecked(true);
viewHolder.firstRadio.setChecked(false);
}
else
{
viewHolder.secondRadio.setChecked(false);
viewHolder.firstRadio.setChecked(false);
}

关于java - 未维护 RadioGroup 状态的自定义 ListView View Holder 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34777385/

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