如何在SharedPreference中保存数据

时间:2014-07-28 18:10:05

标签: android sharedpreferences

我想在SharedPreference中保存ToggleButton!但是不救!我非常努力!但我无法解决它!

有什么问题?

这是代码:

public class Sura extends Activity {
    private MyText text;
        private MyText ttext;
        private SharedPreferences sp;
        private Aye[] ni;
        private ListView lv1;
        private Vibrator vib;
        private Button bshare;
        private Dialog dselect;
        private ToggleButton tbselect;
        private int tempNumber=0;
        private int number;
        private View tempView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.surahmain);

        //dilog box
        dselect=new Dialog(Sura.this);
        dselect.setCancelable(true);
        dselect.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dselect.setContentView(R.layout.dialogstatus);

        tbselect=(ToggleButton) dselect.findViewById(R.id.tbselect);
        bshare=(Button) dselect.findViewById(R.id.bshar);
        bshare.setBackgroundResource(R.drawable.imgb1);

        sp=getSharedPreferences("quran", Activity.MODE_PRIVATE);
        number=getIntent().getExtras().getInt("nsura")+1;
        lv1 = (ListView) findViewById(R.id.custom_list);
        vib=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        text=new MyText(Sura.this,number+".txt");
        ttext=new MyText(Sura.this,"t"+number+".txt");

        text.process();
        ttext.process();


        ArrayList results = new ArrayList();

        ni=new Aye[text.returnString().length];  
            for(int i=0;i<text.returnString().length;i++){
                ni[i]=new Aye(Sura.this);
                ni[i].setarabic(getIntent().getExtras().getString("namesura").toString());
                ni[i].setarabic(text.returnString()[i]);
                ni[i].setnonarabic(ttext.returnString()[i]);

                results.add(ni[i]);
            }

        ArrayList image_details = results;
        lv1.setAdapter(new CustomListAdapter(this, image_details));     
        Load();

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = lv1.getItemAtPosition(position);
                Aye aye = (Aye) o;

            }
        });

            lv1.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View view,
                        int position, long id) {
                    tempNumber=position;
                    tempView=view;
                    vib.vibrate(70);
                    Object o = lv1.getItemAtPosition(position);
                    Aye aye = (Aye) o;
                    tbselect.setChecked(ni[position].isSelected());
                    dselect.show();
                    return false;
                }
            });

            tbselect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    // TODO Auto-generated method stub
                    ni[tempNumber].setSelected(arg1);
                    sp.edit().putBoolean(number+"_"+tempNumber, ni[position].isSelected());
                    sp.edit().commit();
                    if(arg1){
                        tempView.setBackgroundResource(R.drawable.selected);
                    }
                    else {
                        tempView.setBackgroundResource(R.drawable.background);
                    }
                    Log.i("tb", arg1+""+tempNumber);
                }
            });
    }

    public void Load(){
        Log.i("LOAD","LOADED");

        for(int i=0;i<ni.length;i++){
            ni[i].setSelected(sp.getBoolean("a"+number+"_"+i, false));
            Log.i(number+"_"+i,sp.getBoolean("a"+number+"_"+i, false)+"");
            if(ni[i].isSelected()){
                lv1.getChildAt(i).setBackgroundResource(R.drawable.selected);
            }
        }
    }





}

1 个答案:

答案 0 :(得分:1)

尝试改变这一点:

sp.edit().putBoolean(number+"_"+tempNumber, ni[position].isSelected());
sp.edit().commit();

与此:

sp.edit().putBoolean(number+"_"+tempNumber, ni[position].isSelected()).commit();

修改 您正在使用布尔值的键 - 数字+&#34;&#34; + tempNumber - 然后您尝试使用此键检索布尔值 - &#34; a&#34; +数字+&#34; &#34; + i - 。看到&#34; a&#34;!对我来说似乎很奇怪!如果要查找先前存储的值,则2个键必须等于。

希望有所帮助