[英]Save String[][] array in android application with SharedPreferences
我已經閱讀了本主題的相關文章,但無法解決我的問題。 請查看我的代碼,讓我知道問題出在哪里。
我想在我的應用程序中創建一個String [10] [2]數組,因此,每當我的應用程序啟動時,它都會檢索該數組並對其進行處理。 該數組由用戶名和與該用戶名對應的密碼組成。 我想為新用戶創建新密碼,並為已經注冊的用戶檢索密碼。
public class PasswordGeneration extends Activity {
private static final String charSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-/?";
private static Random rnd = new Random();
private String[][] globDataBase = new String[10][2];
private String password;
private String temp;
ImageButton buttonBack;
TextView setPass;
String name,genePass;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pass_result);
buttonBack=(ImageButton)findViewById(R.id.widget54);
setPass = (TextView)findViewById(R.id.widget51);
Bundle bundle = getIntent().getExtras();
name=bundle.getString("name");
name = "Amir";//for testing, this line should be omitted for real application
globDataBase = loadDataBase(this);
genePass = passwordGeneration(name, 12);
setPass.setText(genePass);
buttonBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
public String passwordGeneration(String name, int len) {
for(int i = 0; i < 10; i++){
if(globDataBase[i][0] == name){
return globDataBase[i][1];
}
}
StringBuilder sb = new StringBuilder(len);
for(int j = 0; j < len; j++ )
sb.append(charSet.charAt(rnd.nextInt(charSet.length())));
password = sb.toString();
setDataBase(name, password);
return password;
}
public void setDataBase(String name, String password){
for (int i = 0; i < 10; i++){
if(globDataBase[i][0] == null){
globDataBase[i][0] = name;
globDataBase[i][1] = password;
break;
}
}
saveDataBase(globDataBase, this);
return;
}
public boolean saveDataBase(String[][] db, Context mContext){
SharedPreferences prefs = mContext.getSharedPreferences("dataBase", 0);
Editor editor = prefs.edit();
for(int i = 0; i < 10; i++){
for(int j = 0; j < 2; j++){
editor.putString("dataBase", db[i][j]);
}
}
return editor.commit();
}
public String[][] loadDataBase(Context mContext){
SharedPreferences prefs = mContext.getSharedPreferences("dataBase", 0);
String[][] dataBase = new String[10][2];
for(int i = 0; i < 10; i++){
for(int j = 0; j < 2; j++){
dataBase[i][j] = prefs.getString("dataBase", null);
}
}
return dataBase;
}
}
當我運行我的應用程序時,我得到了用戶名=“ Amir”的密碼,當我再次運行該應用程序時,我希望獲得相同的密碼,但是我得到了一個新名稱相同的“ Amir”。 我不知道問題出在哪里。
for(int i = 0; i < 10; i++){
for(int j = 0; j < 2; j++){
editor.putString("dataBase", db[i][j]);
}
}
這是錯誤的,因為您基本上是在循環並覆蓋鍵“數據庫”的值。
我建議您找到另一種保存數據的方法。 如果仍然要使用共享首選項,則可能最終會為每個元素創建多個鍵,如下所示:
for(int i = 0; i < 10; i++){
for(int j = 0; j < 2; j++){
editor.putString("dataBase["+i+"]["+j+"]", db[i][j]);
}
}
因此您將創建“ dataBase [0] [1]”之類的條目,然后需要邏輯來檢索它們……但是,對我來說,這似乎不是一個最佳解決方案
似乎您可能不知道如何使用鍵值容器。請參閱以下代碼:
//it errors
editor.putString("dataBase", db[i][j]);
保存名稱和密碼后
editor.putString("dataBase", "Amir");
editor.putString("dataBase", "Amirpwd");
你會得到“ Amirpwd”
prefs.getString("dataBase", null)//it returns "Amirpwd"
解:
editor.putString("Amir", "Amirpwd");
您將獲得正確的密碼
String pwd = prefs.getString("Amir", null)//it returns "Amirpwd"
另一個好的方法是將數組序列化為字符串(例如,轉換為JSON字符串),然后使用putString方法將其置於首選項。
當需要獲取它時,可以反序列化序列化的字符串。
您可以使用JSONArray類進行序列化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.