我正在創建一個食譜應用程序,用戶可以根據一種成分搜索食譜(食譜以JSON的形式)。我有一個使用MySQL數據庫(在服務器上)和web服務的工作登錄和註冊功能。我已經實現了一個登錄會話,這樣登錄頁面纔會出現,如果用戶還沒有登錄。如果用戶登錄,則用戶可以訪問他們的帳戶設置和收藏夾列表。如何存儲用戶帳戶的收藏夾?
我現在有一個配方頁面,在操作欄上有一個收藏夾按鈕。點擊後,配方詳細信息應保存到用戶的配置文件中,該配置文件可以從收藏夾列表中訪問和查看。我已經使用SharedPreferences進行登錄會話,但是我仍然可以使用它來存儲用戶配置文件的收藏夾嗎?
所以我的意思是,如果我有一個名爲「test123」的用戶,並且他將一個配方添加到他的收藏夾中,SharedPreferences會爲用戶「test123」存儲此配方嗎?
SessionManager.java
public class SessionManager {
// LogCat tag
private static String TAG = SessionManager.class.getSimpleName();
// Shared Preferences
SharedPreferences pref;
Editor editor;
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "Login";
private static final String KEY_IS_LOGGEDIN = "isLoggedIn";
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
editor.apply();
}
public void setLogin(boolean isLoggedIn) {
editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn);
// commit changes
editor.commit();
Log.d(TAG, "User login session modified!");
}
public boolean isLoggedIn(){
return pref.getBoolean(KEY_IS_LOGGEDIN, false);
}
}
RecipePage.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.favourites:
Toast.makeText(getActivity(), "Added to favourites", Toast.LENGTH_LONG).show();
default:
return super.onOptionsItemSelected(item);
}
}
Favourites.java - (fragment_favs是一個列表視圖)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_favs, container, false);
session = new SessionManager(getActivity());
if(!session.isLoggedIn()){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, new Login());
ft.commit();
}
return rootView;
}
或者我正在考慮將它保存到MySQL數據庫,但我不確定這是否是一種更有效的方法。
我有什麼解決方案可以將收藏添加到用戶個人資料?
通過服務器將其存儲在數據庫中。 SharedPreferences不適用於大量數據。而且您應該可以從服務器訪問該信息,以便他們可以從另一臺設備登錄並仍然可以訪問該信息。 – chRyNaN
@chRyNaN有任何教程可以幫助我解決這個問題嗎? – Theman