如何以编程方式保存单选按钮,滚动时它们不会消失?

时间:2014-10-28 09:38:34

标签: java android

我在检查手机时在本地保存无线电按钮时遇到问题。每次我滚动浏览我的listview时,检查的radiobutton都会消失,我已经检查了互联网,但是10次中有9次使用不同于我的XML文件中的radiobuttons。

public class Bouw_onderdeel extends Activity {
public String[] naam = {"Bouw onderdeel", "Kapconstructie","Kapconstructieve bevesiging","Doorbuiging","Vochtinwerking","Dakconstructie","Constructieve bevesiging","Doorbuiging","Vochtinwerking","Waterkerende lagen","Waterdichtheid (folie)laag","Lekwaterafvoerend vermogen","Detaillering aan dakvoet","Thermischeisolatie","Bevestiging","Aansluitdetails","Isolerend vermogen","Dakpannen en vorsten","Conditie dakpannen en vorsten","Breukschade","Vorstschade","Afschilfering","Aangroei algen en mos"};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bouwonderdeel_layout);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    //RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.layoutBouwonderdeel);  

    ListView list = new ListView(this);
    list.setAdapter(new MyAdapter(this, naam));

    setContentView(list);

}
private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, String[] strings) {
        super(context, -1, -1, strings);
    } 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //Making the listview.
        LinearLayout listLayout = new LinearLayout(Bouw_onderdeel.this);
        listLayout.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.WRAP_CONTENT,
                AbsListView.LayoutParams.WRAP_CONTENT));
        listLayout.setId(5000);
        //Making the radiogroup
        RadioGroup rbg = new RadioGroup(Bouw_onderdeel.this);
        rbg.setId(3927);
        rbg.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.WRAP_CONTENT,
                AbsListView.LayoutParams.WRAP_CONTENT)
                );
        rbg.setOrientation(LinearLayout.HORIZONTAL);
        //Add the textviews inside the listview
        TextView listText = new TextView(Bouw_onderdeel.this);
        listText.setId(5001);
        //For every listview add 5 radiobuttons
        for (int i = 0; i < 5; i++) {
                    RadioButton rbtn = new RadioButton (Bouw_onderdeel.this);
                    rbg.addView(rbtn);
        }

        rbg.setGravity(Gravity.CENTER_HORIZONTAL);       
        rbg.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //Spot where I wanna save I guess?
            }
        });
        listLayout.setGravity(Gravity.CENTER);
        //Add the radiogroup to the listview.
        listLayout.addView(rbg);
        //Add the listtext to the view.
        listLayout.addView(listText);

        listText.setText(super.getItem(position));

        return listLayout;
    }
}

1 个答案:

答案 0 :(得分:0)

按如下方式修改getView方法。

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout listLayout = null;
            if (convertView == null) {
                // Making the listview.
                listLayout = new LinearLayout(Bouw_onderdeel.this);
                listLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
                listLayout.setId(5000);
                // Making the radiogroup
                RadioGroup rbg = new RadioGroup(Bouw_onderdeel.this);
                rbg.setId(3927);
                rbg.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
                rbg.setOrientation(LinearLayout.HORIZONTAL);
                // Add the textviews inside the listview
                TextView listText = new TextView(Bouw_onderdeel.this);
                listText.setId(5001);
                // For every listview add 5 radiobuttons
                for (int i = 0; i < 5; i++) {
                    RadioButton rbtn = new RadioButton(Bouw_onderdeel.this);
                    rbg.addView(rbtn);
                }

                rbg.setGravity(Gravity.CENTER_HORIZONTAL);
                rbg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        // Spot where I wanna save I guess?
                    }
                });
                listLayout.setGravity(Gravity.CENTER);
                // Add the radiogroup to the listview.
                listLayout.addView(rbg);
                // Add the listtext to the view.
                listLayout.addView(listText);

                listText.setText(super.getItem(position));

                convertView = listLayout;
                convertView.setTag(listLayout);
            } else {
                listLayout = (LinearLayout) convertView.getTag();
            }
            return listLayout;
        }