Get Sharedpreferences String Value From Adapter
I'm working now with Android Studio 'Java' and I have question about retrieving value from SharedPreferences. I have stored String value in SharedPreferences in the main activity.
Solution 1:
In your Adapter, you can retrieve SharedPreferences as follows:
SharedPreferencespreferences= context.getSharedPreferences("log11", Context.MODE_PRIVATE);
StringagentId= preferences.getString("agentId", "");
Solution 2:
Pass a context from your adapter constructor and retrieve SharedPreferences instance
publicConversationsAdapter(ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler, Context context) {
mConversationOnClickHandler = conversationOnClickHandler;
pref = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
}
Solution 3:
Pass the context of the calling activity in your adapter through constructor and then use that context :
Context context;
publicConversationsAdapter(Context context,ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler){
this.context=context;
mConversationOnClickHandler = conversationOnClickHandler;
}
Now in your adapter you can do this:
SharedPreferencespreferences= context.getSharedPreferences("log11", Context.MODE_PRIVATE);
StringagentId= preferences.getString("agentId", "default_value");
Post a Comment for "Get Sharedpreferences String Value From Adapter"