2014-02-07 48 views
1

我使用共享首选项来存储动态创建的按钮,并使用它来重命名后存储动态生成的按钮的标签。应用程序工作正常,直到生成按钮,但问题是与标签。如果将三个按钮标记为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(); 
    } 
+0

您使用的是相同的字符串“键”来保存所有按钮的标签被覆盖。您保存的最后一个标签保留在共享首选项中。 – Uttam

+0

@Andrew T.你可以建议编辑该 –

+0

使用“键”+ n - 为第n个按钮作为存储值的关键 – user2450263

回答

0

您正在使用相同的密钥对所有的按钮:

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

您应该创建像KEY1,KEY2和KEY3对于这些不同的密钥。

+0

你可以建议编辑 –

0

其内的环 - (?)

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

“我”就会从for循环

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