Shared Preferences Returning last value after restarting?

99封情书 提交于 2020-01-05 08:01:09

问题


I'm using shared preference to store dynamically created buttons and also using it to store the label of dynamically generated buttons after renaming them. Application works fine till generating buttons but the problem is with labeling. If Label three buttons as Test1, Test2, Test3 and so on. But after restarting application all the generated buttons have label Test3 on them.Code in MainActivity

 SharedPreferences prefs=null;
int count = 0;

"Code in onCreate method"

prefs = PreferenceManager.getDefaultSharedPreferences(this);
    count=prefs.getInt("count", 0);
    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
    for(int i=0;i<count;i++)
        {
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
            final Button myButton = new Button(this);
            myButton.setOnClickListener(new OnClickListener() 
                {
                    @Override
                    public void onClick(View v)
                        {
                            reportDialog(myButton.getText().toString());
                        }
                });

            myButton.getId();
            myButton.setText(prefs.getString("key","New"));
            myButton.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View arg0)
                    {
                        AlertDialog lbldialog = new AlertDialog.Builder(context).create();
                        lbldialog.setTitle("Change Button Label");
                        lbldialog.setIcon(android.R.drawable.ic_dialog_info);   
                        lbldialog.setMessage("Enter new Button label  to change");
                        final EditText input = new EditText(MainActivity.this);                 
                        lbldialog.setView(input);
                        lbldialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) 
                                    {
                                        myButton.setText(input.getText());
                                        Editor edit = prefs.edit();
                                        edit.putString("key", myButton.getText().toString());
                                        edit.commit();
                                    }
                            });

                        lbldialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
                        new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) 
                                    {
                                        Toast.makeText(getApplicationContext(), "Button Label not Changed",Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }
                            });
                        lbldialog.show();   
                return true;  
                }
        });
        ll.addView(myButton, lp);
    }

"Code to add new buttons:"

if(v == btnaddnew)  

{    final Button btn1 = new Button(this);
btn1.setText("New");
btn1.setId(23);

btn1.setOnClickListener(new OnClickListener () {
    @Override
    public void onClick(View v){
        rptDialog(btn1.getText().toString());
        }
    })
btn1.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
        //Dialog Box pops up with edit text field to change button label
        AlertDialog lbldialog = new AlertDialog.Builder(context).create();
        lbldialog.setTitle("Change Button Label");
        lbldialog.setIcon(android.R.drawable.ic_dialog_info);   
        lbldialog.setMessage("Enter new Button label  to change");
        final EditText input = new EditText(MainActivity.this);
        lbldialog.setView(input);
        lbldialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change",
                    new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                    {
                        btn1.setText(input.getText());
                        Editor edit = prefs.edit();
                        edit.putString("key", btn1.getText().toString());
                        edit.commit();

                    }
            });

        lbldialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
                    new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Button Label not Changed",Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    }
                });
        lbldialog.show();   
    return true; 
     }
    });         
    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
    ll.addView(btn1, lp);
    count++;
    Editor editor = prefs.edit();
    editor.putInt("count", count);
    editor.commit();
    }

回答1:


You are using same key for all the buttons:

btn1.setText(input.getText());
Editor edit = prefs.edit();
edit.putString("key", btn1.getText().toString());
edit.commit();

You should create different keys like key1, key2 and key3 for each of these.




回答2:


Editor edit = prefs.edit();
edit.putString("key1", myButton.getText().toString());
edit.commit();

Editor edit = prefs.edit();
edit.putString("key2", btn1.getText().toString());
edit.commit();



回答3:


as its inside the for loop- (?)

edit.putString("key"+i, myButton.getText().toString());  

the "i" will be from the for loop then



来源:https://stackoverflow.com/questions/21620270/shared-preferences-returning-last-value-after-restarting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!