共享preferences重启后返回一个值? [英] Shared Preferences Returning last value after restarting?

查看:148
本文介绍了共享preferences重启后返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的共享preference来存储动态创建按钮,并用它来动态生成按钮的标签存储重命名后的他们。应用程序工作正常,直到生成按钮,但问题是标签。如果标签三个按钮为Test1,Test2的,Test3的等等。但在重新启动应用程序后,所产生的所有按钮具有标签Test3的他们。 code在MainActivity

 共享preferences preFS = NULL;
诠释计数= 0;code在onCreate方法preFS = preferenceManager.getDefaultShared preferences(本);
    数= prefs.getInt(计数,0);
    LL的LinearLayout =(的LinearLayout)findViewById(R.id.layout1);
    的for(int i = 0; I<计数;我++)
        {
            的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            最终按钮myButton的=新按钮(本);
            myButton.setOnClickListener(新OnClickListener()
                {
                    @覆盖
                    公共无效的onClick(视图v)
                        {
                            reportDialog(myButton.getText()的toString());
                        }
                });            myButton.getId();
            myButton.setText(prefs.getString(键,新));
            myButton.setOnLongClickListener(新OnLongClickListener(){
                公共布尔onLongClick(查看arg0中)
                    {
                        AlertDialog lbldialog =新AlertDialog.Builder(上下文).create();
                        lbldialog.setTitle(更改按钮标签);
                        lbldialog.setIcon(android.R.drawable.ic_dialog_info);
                        lbldialog.setMessage(进入新的按钮标签变);
                        最终的EditText输入=新的EditText(MainActivity.this);
                        lbldialog.setView(输入);
                        lbldialog.setButton(DialogInterface.BUTTON_POSITIVE,更改,新DialogInterface.OnClickListener(){
                                公共无效的onClick(DialogInterface对话,诠释其)
                                    {
                                        myButton.setText(input.getText());
                                        编辑编辑= prefs.edit();
                                        edit.putString(钥匙,myButton.getText()的toString());
                                        edit.commit();
                                    }
                            });                        lbldialog.setButton(DialogInterface.BUTTON_NEGATIVE,取消,
                        新DialogInterface.OnClickListener(){
                                @覆盖
                                公共无效的onClick(DialogInterface对话,诠释其)
                                    {
                                        Toast.makeText(getApplicationContext()按钮标签没有改变,Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }
                            });
                        lbldialog.show();
                返回true;
                }
        });
        ll.addView(myButton的,LP);
    }code以添加新的按钮:如果(V == btnaddnew){最后按钮BTN1 =新按钮(本);
btn1.setText(新);
btn1.setId(23);btn1.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        rptDialog(btn1.getText()的toString());
        }
    })
btn1.setOnLongClickListener(新OnLongClickListener(){
    公共布尔onLongClick(查看为arg0){
        //弹出对话框与编辑文本字段更改按钮标签
        AlertDialog lbldialog =新AlertDialog.Builder(上下文).create();
        lbldialog.setTitle(更改按钮标签);
        lbldialog.setIcon(android.R.drawable.ic_dialog_info);
        lbldialog.setMessage(进入新的按钮标签变);
        最终的EditText输入=新的EditText(MainActivity.this);
        lbldialog.setView(输入);
        lbldialog.setButton(DialogInterface.BUTTON_POSITIVE,更改,
                    新DialogInterface.OnClickListener()
            {
                公共无效的onClick(DialogInterface对话,诠释其)
                    {
                        btn1.setText(input.getText());
                        编辑编辑= prefs.edit();
                        edit.putString(钥匙,btn1.getText()的toString());
                        edit.commit();                    }
            });        lbldialog.setButton(DialogInterface.BUTTON_NEGATIVE,取消,
                    新DialogInterface.OnClickListener(){                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){
                    Toast.makeText(getApplicationContext()按钮标签没有改变,Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    }
                });
        lbldialog.show();
    返回true;
     }
    });
    LL的LinearLayout =(的LinearLayout)findViewById(R.id.layout1);
    的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    ll.addView(BTN1,LP);
    算上++;
    编辑编辑= prefs.edit();
    editor.putInt(计数,计数);
    editor.commit();
    }


解决方案

作为其内,用于环路(?)

  edit.putString(键+ I,myButton.getText()的toString());

我就会从for循环

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();
    }

解决方案

as its inside the for loop- (?)

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

the "i" will be from the for loop then

这篇关于共享preferences重启后返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆