【问题标题】:Android Radio buttons in a custom listview changes its state when we scroll the list当我们滚动列表时,自定义列表视图中的 Android 单选按钮会更改其状态
【发布时间】:2013-09-21 11:07:43
【问题描述】:

我是一个初学者,面临一个问题,我正在创建一个带有两个 RadioButtons 和一个 TextViewListView。一切都很好,但是当我设置 RadioButtons 并滚动它时,以前的 RadioButtons 的值会改变它们的值或显着失去它们的状态。我试图解决这个问题很长时间。我应该做出哪些改变来克服这个问题?

MainActivity.java

public class MainActivity extends Activity {

     private ListView listView1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Option weather_data[] = new Option[]
                    {
                        new Option("Heading1"),
                        new Option("Heading12"),
                        new Option("Heading3"),
                        new Option("Heading4"),
                        new Option("Heading5"),
                        new Option("Heading6"),
                        new Option("Heading7"),
                        new Option("Heading8"),
                        new Option("Heading9"),
                        new Option("Heading10")
                    };
            RadioGroupAdapter adapter = new RadioGroupAdapter(this, 
                            R.layout.listitem, weather_data);
                    listView1 = (ListView)findViewById(R.id.list);
                    listView1.setAdapter(adapter);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }

Option.java

public class Option {
     public String title;
        public Option(){
            super();
        }

        public Option( String title) {
            super();
            this.title = title;
        }
    }

RadioGroupAdapter.java

public class RadioGroupAdapter extends ArrayAdapter<Option> {

    Context context;
    int layoutResourceId;
    Option data[] = null;

    public RadioGroupAdapter(Context context, int layoutResourceId,
            Option[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MatrixHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MatrixHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.heading);
            holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
            final RadioButton[] rb = new RadioButton[2];
            for(int i=0; i<2; i++){
                rb[i]  = new RadioButton(context);
                //rb[i].setButtonDrawable(R.drawable.single_radio_chice);
                rb[i].setId(i);
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT);
                params.weight=1.0f;
                params.setMargins(5, 0, 5, 10);
                holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
            }
            row.setTag(holder);
        } else {
            holder = (MatrixHolder) row.getTag();
        }

        Option option = data[position];
        holder.txtTitle.setText(option.title);
        return row;
    }

    static class MatrixHolder {
        TextView txtTitle;
        RadioGroup group;
        int position;
    }
}

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:id="@+id/heading"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:padding="10dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_weight="50" />

    <RadioGroup
        android:id="@+id/radio_group1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="horizontal"
        android:layout_weight="50" >
    </RadioGroup>

</LinearLayout>

【问题讨论】:

    标签: android listview radio-button adapter radiobuttonlist


    【解决方案1】:

    您需要为每个项目保存 RadioButton 的状态,因此在滚动时 首先在 getView() 方法中检查每个 RadioButton 的状态。

    Step1:- 你需要创建一个 Pojo 类(Setter & Getter) 来保存 RadioButton 的状态。

    更多详情请点击以下链接

    http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html

    这个例子是一个带有 CheckBox 的 ListView,你需要根据你的要求把它改成 RadioButton。

    【讨论】:

      【解决方案2】:

      您的适配器总是为列表视图重新创建项目。 当他这样做时,他会获得一个“新鲜”的视图,您必须使用您的数组将数据设置到此视图。

      在您的情况下,如果您想“保存”单选按钮的状态,您必须将此数据保存为适配器数据的一部分,即在数组中。 我建议您向 Option 对象添加一个字段,并使用它保存单选按钮的最后一个值。之后,当他们调用Option option = data[position]; 行时,您可以回收最后一个值并将其显示在屏幕上。

      【讨论】:

      • 谢谢你,伙计。你能告诉我怎么做吗?
      • 1.您应该注册单选按钮更改侦听器 (setOnCheckedChangeListener),并且在更改时 - 然后将该数据保存到当前数据对象。 2. 每次调用getView - 您检查数据对象是否包含收音机的某些状态,并根据它设置 RadioButton 当前状态
      • @肖恩:是的,我明白了。如果可能的话,你能编辑我的代码吗?因为我真的很困惑。
      • @肖恩:是的,我明白了。如果可能的话,你能编辑我的代码吗?因为我真的很困惑。请在编辑代码时通知我。
      【解决方案3】:

      在下面添加到您的适配器 2 抽象函数;

      @Override
      public int getCount() {
          return data.size(); // size, lenght, count ...?
      }
      
      @Override
      public Object getItem(int position) {
          return position;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-17
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 2013-03-28
        • 1970-01-01
        • 2020-04-14
        • 2012-02-04
        相关资源
        最近更新 更多