刷新列表或重新打开应用程序时如何检查切换按钮状态

时间:2014-05-07 07:59:17

标签: android listview sharedpreferences baseadapter togglebutton

我的应用包含一个列表视图,该视图由微调器值填充 此列表包含每行中的切换按钮,并且为了自定义目的,我在列表视图中使用了baseadapter

我正在更改已选中和未选中的切换按钮的图像 切换按钮和列表视图的功能正常,但问题发生在我关闭应用程序然后再次打开它时会刷新并且切换状态变为未选中状态并且每个位置都显示未检查的图像

所以要解决这个问题,我已经在共享首选项中保存了切换按钮的值,但是现在我不知道在哪里检查这个基础适配器类plz帮帮我

的必要代码
public class DDListAdapter extends BaseAdapter {

        ArrayList<DataModelDD> listArray;
        int curIndex=1000, pIndex;

        public DDListAdapter(String[] str, String[] str1) {
            listArray = new ArrayList<DataModelDD>();

            for (int i=0; i < str.length; i++) {
                listArray.add(new DataModelDD(str[i],str1[i], " Day Alert on " + str[i],false));    
            }   

        }

        public void DDListUpdate(String[] str, String[] str1){
            listArray = new ArrayList<DataModelDD>();
            for (int i=0; i < str.length; i++) {
                listArray.add(new DataModelDD(str[i],str1[i], " Day Alert on " + str[i],false));    

            }
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return listArray.size(); // total number of elements in the list
        }

        @Override
        public Object getItem(int i) {

            return listArray.get(i); // single item in the list

        }

        @Override
        public long getItemId(int i) {

            return i; // index number

        }

        @Override
        public View getView(final int index, View view, final ViewGroup parent) {
            lIndex = index;
            pIndex = index;


            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.lstdd, parent, false);

            final DataModelDD dmFl = listArray.get(index);

            final TextView lbl1 = (TextView) view.findViewById(R.id.txtDDate);
            final TextView lbl2 = (TextView) view.findViewById(R.id.txtDres);

            lbl1.setText(dmFl.getDDate());
            lbl2.setText(dmFl.getDres());


            final ToggleButton btnlock = (ToggleButton) view.findViewById(R.id.btnDAlarm);

            if (dmFl.getdSel()) {
                //selected
                btnlock.setButtonDrawable(a_icon);


            } else {
                //not selected
                btnlock.setButtonDrawable(a_dicon);
            }




            btnlock.setTag(pIndex);
            btnlock.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(btnlock.isChecked()){

                        btnlock.setButtonDrawable(a_icon);
                        btnlock.setChecked(true); 
                        dmFl.setdSel(true);

                         Integer position = (Integer) buttonView.getTag();


                         sp = context.getSharedPreferences("MyPref", 0);
                         SharedPreferences.Editor editor = sp.edit();
                         editor.putBoolean("value"+month+"_"+state+"_"+cday, true);
                         editor.commit();          

                        }


                    else{

                        final String alarmTime = dmFl.getDDate();
                        disableAlarm(buttonView,alarmTime);
                        btnlock.setButtonDrawable(a_dicon);
                        btnlock.setChecked(false); 
                        dmFl.setdSel(false);

                        Integer position = (Integer) buttonView.getTag();

                    }

                }


            });


            return view;
        }
    }


and the data model class
package com.example.dd;

public class DataModelDD {

    private String DDate;
    private String Dres;
    private String ShrStr;  
    private Boolean dSel;

    public DataModelDD(String DDate, String Dres, String ShrStr, Boolean dSel){
        this.DDate = DDate;
        this.Dres = Dres;
        this.ShrStr = ShrStr;
        this.dSel = dSel;
    }

    public String getDDate(){
        return this.DDate;
    }

    public void setDDate(String DDate){
        this.DDate = DDate;
    }

    public String getDres(){
        return this.Dres;
    }

    public void setDres(String Dres){
        this.Dres = Dres;
    }

    public String getShrStr(){
        return this.ShrStr;
    }

    public void setShrStr(String ShrStr){
        this.ShrStr = ShrStr;
    }

    public Boolean getdSel(){
        return this.dSel;
    }

    public void setdSel(Boolean dSel){
        this.dSel = dSel;
    }

}

1 个答案:

答案 0 :(得分:0)

你需要做

SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
boolean rb0 = prefs.getBoolean("rb0", true);

if (dmFl.getdSel()||rb0) {
            //selected
            btnlock.setButtonDrawable(a_icon);


        } else {
            //not selected
            btnlock.setButtonDrawable(a_dicon);
        }