共享首选项重新启动后返回最后一个值?

时间:2014-02-07 05:50:16

标签: android sharedpreferences dynamically-generated

我正在使用共享首选项来存储动态创建的按钮,并在重命名后使用它来存储动态生成按钮的标签。应用程序工作正常,直到生成按钮,但问题是标签。如果Label三个按钮分别为Test1,Test2,Test3等。但是在重新启动应用程序后,所有生成的按钮都有标签Test3。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();
    }

3 个答案:

答案 0 :(得分:0)

您对所有按钮使用相同的键:

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

您应该为每个键创建不同的键,如key1,key2和key3。

答案 1 :(得分:0)

作为for循环内部 - (?)

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

“i”将来自for循环

答案 2 :(得分:0)

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

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