5

私は初心者で、ListView2 つRadioButtonの と を使用して を作成するという問題に直面していTextViewます。すべてうまくいきますが、RadioButtons を設定してスクロールすると、以前RadioButtonの s の値が値を変更したり、状態が劇的に失われたりします。私は長い間この問題を解決しようとしています。この問題を克服するには、どのような変更を加える必要がありますか?

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>
4

3 に答える 3

3

各項目の RadioButton の状態を保存する必要があるため、最初に getView() メソッドでスクロールしているときに、各 RadioButton の状態がチェックされます。

Step1:- RadioButton の状態を保存するための Pojo クラス (Setter & Getter) を作成する必要があります。

詳細については、以下のリンクを参照してください

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

この例は、要件に応じて RadioButton に変更する必要がある CheckBox を含む ListView です。

于 2013-09-17T06:33:01.610 に答える
2

アダプタは常にリスト ビューのアイテムを再作成しています。彼がそれを行うとき、彼は「新鮮な」ビューを取り、配列を使用してこのビューにデータを設定する必要があります。

あなたの場合、ラジオボタンの状態を「保存」したい場合は、このデータをアダプターデータの一部として、つまり配列に保存する必要があります。Optionオブジェクトにフィールドを追加し、それを使用してラジオ ボタンの最後の値を保存することをお勧めします。その後、それらの行Option option = data[position];が呼び出されたときに、最後の値を再利用して画面に表示できます。

于 2013-09-17T06:35:08.353 に答える
0

以下のアダプタ 2 抽象関数に追加します。

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

@Override
public Object getItem(int position) {
    return position;
}
于 2016-03-27T17:28:38.117 に答える