2014-02-07 1 views
1

나는 공유 환경 설정을 사용하여 동적으로 생성 된 버튼을 저장하고 이름을 변경 한 후에 동적으로 생성 된 버튼의 레이블을 저장하는 데 사용하고 있습니다. 응용 프로그램은 버튼을 생성 할 때까지 제대로 작동하지만 레이블링 문제가 있습니다. 레이블이 세 개이면 Test1, Test2, Test3 등으로 표시됩니다. 그러나 응용 프로그램을 다시 시작한 후에는 생성 된 모든 단추에 레이블 Test3이 있습니다.공유 기본 설정 다시 시작한 후 마지막 값을 반환 하시겠습니까?

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

당신은 저장 동일한 문자열 "키"를 사용하는의에서됩니다 루프 인 (?)에 대해 모든 단추의 레이블을 덮어 씁니다. 마지막으로 저장 한 레이블은 sharedpreferences에 유지됩니다. – Uttam

+0

@Andrew T. 그 –

+0

에 대한 편집을 제안 할 수 있습니다. "key"+ n - 값을 저장하기위한 키로 n 번째 버튼에 대해 – user2450263

답변

0

Code in MainActivity 당신은 모든 버튼에 동일한 키를 사용 :

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

는 이러한 각 키 1, 키 2 및 KEY3 같은 다른 키를 만들어야합니다. 같은

+0

그 편집 내용을 제안 할 수 있습니까? –

0

의 내부

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

은 "내가"루프 다음 ​​

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(); 
관련 문제