2017-12-13 1 views
0

내 활동 oncreate에서 공유 환경 설정을 사용하여 저장 버튼 클릭시 데이터를 저장하기위한 edittext 및 버튼을 정의했지만 데이터가 저장되지 않음 다시 같은 활동으로, 나는 왜 그것이 저장되지 않는지 이해하지 못한다. 여기 내 전체 코드입니다. 디버깅하는 동안 나는이 액티비티에 저장되는 것을 보았습니다.이 액티비티가 끝나면 캐시가 지워집니다.Shared Prefrence 캐시로 텍스트를 저장하지 않음 android

public class MyActivity extends AppCompatActivity { 
    public EditText name1, name2, name3, name4, name5, name6, name7, name8, name9, name10, phoneName, phoneContact, contact1, contact2, contact3, contact4, contact5, contact6, contact7, contact8, contact9, contact10; 
    Button saveButton; 
    private Context context; 
    public SharedPreferences.Editor editorcache; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sos); 

     context = this.getApplicationContext(); 

     saveButton = (Button) findViewById(R.id.save); 

     name1 = ((EditText) findViewById(R.id.name1)); 
     name2 = ((EditText) findViewById(R.id.name2)); 
     name3 = ((EditText) findViewById(R.id.name3)); 
     name4 = ((EditText) findViewById(R.id.name4)); 
     name5 = ((EditText) findViewById(R.id.name5)); 
     name6 = ((EditText) findViewById(R.id.name6)); 
     name7 = ((EditText) findViewById(R.id.name7)); 
     name8 = ((EditText) findViewById(R.id.name8)); 
     name9 = ((EditText) findViewById(R.id.name9)); 
     name10 = ((EditText) findViewById(R.id.name10)); 

     contact1 = ((EditText) findViewById(R.id.contact1)); 
     contact2 = ((EditText) findViewById(R.id.contact2)); 
     contact3 = ((EditText) findViewById(R.id.contact3)); 
     contact4 = ((EditText) findViewById(R.id.contact4)); 
     contact5 = ((EditText) findViewById(R.id.contact5)); 
     contact6 = ((EditText) findViewById(R.id.contact6)); 
     contact7 = ((EditText) findViewById(R.id.contact7)); 
     contact8 = ((EditText) findViewById(R.id.contact8)); 
     contact9 = ((EditText) findViewById(R.id.contact9)); 
     contact10 = ((EditText) findViewById(R.id.contact10)); 

     phoneName = ((EditText) findViewById(R.id.phoneName)); 
     phoneContact = ((EditText) findViewById(R.id.phoneContact)); 

     saveButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
       public void onClick(View v) { 
       SharedPreferences sharedPrefcache = context.getSharedPreferences("Details", Context.MODE_PRIVATE); 
       editorcache = sharedPrefcache.edit(); 
       editorcache.putString("name1", name1.getText().toString()); 
       editorcache.putString("name2", name2.getText().toString()); 
       editorcache.putString("name3", name3.getText().toString()); 
       editorcache.putString("name4", name4.getText().toString()); 
       editorcache.putString("name5", name5.getText().toString()); 
       editorcache.putString("name6", name6.getText().toString()); 
       editorcache.putString("name7", name7.getText().toString()); 
       editorcache.putString("name8", name8.getText().toString()); 
       editorcache.putString("name9", name9.getText().toString()); 
       editorcache.putString("name10", name10.getText().toString()); 
       editorcache.putString("contact1", contact1.getText().toString()); 
       editorcache.putString("contact2", contact2.getText().toString()); 
       editorcache.putString("contact3", contact3.getText().toString()); 
       editorcache.putString("contact4", contact4.getText().toString()); 
       editorcache.putString("contact5", contact5.getText().toString()); 
       editorcache.putString("contact6", contact6.getText().toString()); 
       editorcache.putString("contact7", contact7.getText().toString()); 
       editorcache.putString("contact8", contact8.getText().toString()); 
       editorcache.putString("contact9", contact9.getText().toString()); 
       editorcache.putString("contact10",contact10.getText().toString()); 
       editorcache.putString("phoneName",phoneName.getText().toString()); 
       editorcache.putString("phoneContact",phoneContact.getText().toString()); 
       editorcache.commit(); 
       finish(); 
      } 
     }); 
    } 
} 
+3

포스트 전체 코드 데이터를 가져와'ContactDetails' –

+0

가 어떻게 이것들을 검색하려고 않았다 없음 오타의 확인? 코드를 공유하십시오. –

+0

그것은 코드를 제공하는 것 같습니다. SharedPreference에서 데이터를 가져 오는 방법을 보여주십시오. – Kuls

답변

0

이동 외부의 onclick 방법으로이 코드는 내가

SharedPreferences sharedPrefcache = context.getSharedPreferences("Details", Context.MODE_PRIVATE); 
editorcache = sharedPrefcache.edit(); 
+0

나는 너무 그것을 시도했다 .. 행운을 빌어 요 –

+0

당신은 전체 코드를 공유 할 수 있습니다. –

+1

이미 전체 코드 –

0

그냥 된 SharedPreferences에 대답 아래

저장 데이터를 확인 당신이 그 세부 사항을 얻을 것이다 생각

SharedPreferences sharedPrefcache = 
MainActivity.this.getSharedPreferences("Details", Context.MODE_PRIVATE); 
SharedPreferences.Editor editorcache = sharedPrefcache.edit(); 
editorcache.putString("id", "123"); 
editorcache.putString("name", "nikhil"); 
editorcache.commit(); 
finish(); 

된 SharedPreferences에서

String id = sharedPrefcache.getString("id",""); 
String name = sharedPrefcache.getString("name",""); 
+0

하드 코드되지 않은 My EditText의 데이터를 원한다. –

+0

@SaurabhYadav 내 하드 코딩 된 값 대신 'editorcache.putString ("name1", name1.getText(). toString()); editorcache.putString ("name2", name2.getText(). toString()); ' –

+0

나는 이미 알고있는 것과 똑같은 점을 알고 있습니다. 차이점은 무엇입니까? @Nikhil Lotke –