2012-03-20 2 views
2

Android의 sharedpreferences에서 사용자 이름과 비밀번호를 검색하는 데 문제가 있습니다. 나는 사용자 이름을 저장하고Android 공유 환경 설정에서 사용자 이름과 비밀번호를 가져옵니다.

SharedPreferences prefs=getSharedPreferences("File", 0); 
    SharedPreferences.Editor e= prefs.edit(); 
     e.putString("Email", "[email protected]").putString("Password", "password1"); 
     e.commit(); 
     e.putString("Email", "[email protected]").putString("Password", "password2"); 
     e.commit(); 
     String s=prefs.getString("Email","not found"); 

를 전달하는 데이 코드를 사용하지만 난 로그인하는 사용자에 대한 정보를 검색하는 방법을 잘 모릅니다. 아무도 나에게 당신은 서로 다른 값에 대해 서로 다른 키를 줄 필요가

+0

간단한 개념을, 그것을 봐 쪽으로. – JoxTraex

답변

5

공유 환경 만들기 : 공유 환경에서

SharedPreferences sp=getSharedPreferences("Login", 0); 
SharedPreferences.Editor Ed=sp.edit(); 
Ed.putString("Unm",Value);    
Ed.putString("Psw",Value); 
Ed.commit(); 

가져 오기 값 : 자바 해시 맵의

SharedPreferences sp1=this.getSharedPreferences("Login",null); 

String unm=sp1.getString("Unm", null);  
String pass = sp1.getString("Psw", null); 
+0

예, 전 단지 이드를 알아 냈습니다. 고맙다 :) 이것은 내가 찾고 있었던 해결책이다! – Darko

+0

괜찮아요. 친애하는 당신에게 유용하다면 대답을 받아들이십시오. 그러면 다른 사람들에게도 도움이 될 것입니다 .. – Hasmukh

+0

나는 동일한 패키지에 드라이버 앱과 라이더 앱을 가지고 있습니다. 드라이버 앱에서 현재의 라이더 ID를 얻으려고합니다. 위에서 한 것처럼 행동했지만 riderId가 null로 반환되는 이유는 무엇입니까? 내가 도대체 ​​뭘 잘못하고있는 겁니까? – LizG

0

를 알아낼 수 있습니다 그렇지 않으면 두 번째 전자 메일이 첫 번째 전자 메일을 지 웁니다. 지속적인 해시 맵으로 공유 환경 설정을 참조하십시오

//keep constants, don't use their values. A constant has more meaning 
    SharedPreferences prefs=getSharedPreferences("File", MODE_PRIVATE); 
    SharedPreferences.Editor e= prefs.edit(); 
    //keys should be constants as well, or derived from a constant prefix in a loop. 
    e.putString("Email1", "[email protected]").putString("Password1", "password1"); 
    e.putString("Email2", "[email protected]").putString("Password2", "password2"); 
    //commit once, not twice 
    e.commit(); 

    //not found should be a constant in a xml resource file 
    String mail1=prefs.getString("Email1","not found"); 
    String mail2=prefs.getString("Email2","not found"); 
관련 문제