在android應用程序中我有列表視圖,您可以在其中添加新對象,每個「對象」都有4-5個字符串值。 我不預測任何人可以在應用程序中使用更多3或4個這些對象。至於現在它在數據庫上創建,SQLite,一個對象=一個記錄,具有4-5個值(類型爲TEXT),但它越來越難以維護,我認爲它增加了應用程序的迂迴性。用於存儲ListView數據的共享首選項
是否可以通過共享首選項完成?或者存儲這些數據不是個好主意? 鍵和值如何,我可以讓他們去旅行嗎?
在android應用程序中我有列表視圖,您可以在其中添加新對象,每個「對象」都有4-5個字符串值。 我不預測任何人可以在應用程序中使用更多3或4個這些對象。至於現在它在數據庫上創建,SQLite,一個對象=一個記錄,具有4-5個值(類型爲TEXT),但它越來越難以維護,我認爲它增加了應用程序的迂迴性。用於存儲ListView數據的共享首選項
是否可以通過共享首選項完成?或者存儲這些數據不是個好主意? 鍵和值如何,我可以讓他們去旅行嗎?
您需要Gson將對象放入共享首選項。您可以找到它here以及如何將它添加到您的項目,如this。不要忘記在gradle文件'build.gradle'compile files ('libs/gson-2.2.4.jar')
中添加依賴項。
爲了把對象共享偏好使用以下命令:
SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String serializedObj = gson.toJson(ObjectHere);
editor.putString("key", serializedObj);
editor.commit();
檢索數據後執行下列操作
SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);
Gson gson = new Gson();
String serializedObj = preferences.getString("key", "DEFAULT");
ObjectType object = gson.fromJson(serializedObj, Object.class);
你可以寫數據作爲文件,然後使用文件名需要時檢索它:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
這將是更好,如果ü在後臺線程例如一個的AsyncTask執行此任務。
您可以隨時添加,更新或刪除共享首選項,如果您使用sharedpreference,它會更容易。
/*******創建SharedPreferences *******/
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
/********存儲的數據作爲鍵/值對*******************/
editor.putBoolean("key_name1", true); // Saving boolean - true/false
editor.putInt("key_name2", "int value"); // Saving integer
editor.putFloat("key_name3", "float value"); // Saving float
editor.putLong("key_name4", "long value"); // Saving long
editor.putString("key_name5", "string value"); // Saving string
// Save the changes in SharedPreferences
editor.commit(); // commit changes
/****************獲取SharedPreferences data *******************/
//如果key的值不存在,則返回第二個參數值 - 在這種情況下爲null
pref.getBoolean("key_name1", null); // getting boolean
pref.getInt("key_name2", null); // getting Integer
pref.getFloat("key_name3", null); // getting Float
pref.getLong("key_name4", null); // getting Long
pref.getString("key_name5", null); // getting String
/************從SharedPreferences刪除密鑰值*****************/
editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4
// Save the changes in SharedPreferences
editor.commit(); // commit changes
/************清除SharedPreferences中的所有數據*****************/
editor.clear();
editor.commit(); // commit changes
它可以通過GSON(https://github.com/google/gson)來解決。只需在您的gradle dependencies部分添加compile files ('libs/gson-2.2.4.jar')
即可。
假設您使用列表視圖的 class YOUR_CLASS
Arraylist。即
private ArrayList<YOUR_CLASS> arraylist = new ArrayList<YOUR_CLASS>();
現在加入項目對象後,您arraylist
,你可以用它保存在共享偏好:
SharedPreferences preferences = getSharedPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String arraylist_in_string = gson.toJson(arraylist).toString(); //Converting it to String
editor.putString("YOUR_KEY", arraylist_in_string); //now saving the String
editor.commit();
現在,當您需要使用arraylist
你可以把它從sharedPreferences使用
Gson gson = new Gson();
String arraylist_in_string = preferences.getString("YOUR_KEY", null);
if(arraylist_in_string != null) //if we have list saved
{
//creating a list of YOUR_CLASS from the string response
Type type = new TypeToken<ArrayList<YOUR_CLASS>>() {}.getType();
List<YOUR_CLASS> list = new Gson().fromJson(arraylist_in_string, type);
//now adding the list to your arraylist
if (list != null && list.size() > 0)) {
arraylist.clear();// before clearing check if its not null.
arraylist.addAll(list);
}else //ie list isn't saved yet
{
//You may want to save the arraylist into Shared preference using previous method
}
不,共享首選項不能用於存儲較大的對象,這是一件壞事。 [開發者網站](http://developer.android.com/guide/topics/data/data-storage.html#pref) –