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

标签 java android

我已经使用 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/

相关文章:

android - 如何在 uiautomator 中使用 getApplicationContext()?

android - Oxygen os 4.1.7 和 android V7.1.1 的一加 3 手机不生成百度推送通知 channel id

java - BundleContext 在使用 pax-exam 的单元测试中为 null

java - Jersey 客户端 - 如何通过 POST 请求以表单形式发送列表

java - Java应用程序将无法播放声音

java - 释放 JNI 数组

android - 如何在 UITest 中检查按钮的颜色

android - 工具栏不会隐藏在列表滚动中

java - 如何启动 WildFly8 服务器(在 OpenShift 中)

java - 小数点后无法仅显示 1 位或空位