android - 从适配器获取 SharedPreferences 字符串值
问题描述
我现在正在使用 Android Studio 'Java',我对从 SharedPreferences 中检索值有疑问。
我已将字符串值存储在主要活动的 SharedPreferences 中。
现在我需要从我的适配器中获取值才能使用它。
我尝试了很多解决方案,但我得到了错误。
这就是我存储值的方式:
public void saveAgentId() {
SharedPreferences settings = getSharedPreferences("log11",0);
String firstRun = settings.getString("agentId", null);
if(firstRun == null)//if running for first time
{
SharedPreferences.Editor editor = settings.edit();
editor.putString("agentId", agentId);
editor.apply();
} else {
SharedPreferences.Editor editor = settings.edit();
editor.putString("agentId", agentId);
editor.apply();
}
}
这是我的适配器:
public class ConversationsAdapter extends
RecyclerView.Adapter<ConversationsAdapter.ConversationsAdapterViewHolder> {
private Context context;
private ArrayList<Conversation.Data> mConversation;
private ConversationsAdapter.ConversationOnClickHandler mConversationOnClickHandler;
private static SharedPreferences pref;
public ConversationsAdapter(ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler) {
mConversationOnClickHandler = conversationOnClickHandler;
}
public void setConversationData(ArrayList<Conversation.Data> conversation) {
mConversation = conversation;
notifyDataSetChanged();
}
public void addAll(ArrayList<Conversation.Data> newList) {
int lastIndex = mConversation.size() - 1;
mConversation.addAll(newList);
notifyItemRangeInserted(lastIndex, newList.size());
}
@Override
public ConversationsAdapter.ConversationsAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.row_msg, parent, false);
// Return a new holder instance
ConversationsAdapter.ConversationsAdapterViewHolder viewHolder = new ConversationsAdapter.ConversationsAdapterViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ConversationsAdapter.ConversationsAdapterViewHolder viewHolder, int position) {
Conversation.Data conversation = mConversation.get(position);
TextView tv1 = viewHolder.tv1;
tv1.setText(conversation.getBody());
}
// Returns the total count of items in the list
@Override
public int getItemCount() {
if(mConversation == null) {
return 0;
}
return mConversation.size();
}
public class ConversationsAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView tv1;
public ConversationsAdapterViewHolder(View view) {
super(view);
tv1 = (TextView) view.findViewById(R.id.tvMsgR);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Conversation.Data selectedNotifiction = mConversation.get(position);
mConversationOnClickHandler.onClickConversation(selectedNotifiction);
}
}
public interface ConversationOnClickHandler {
void onClickConversation(Conversation.Data conversation);
}
public long myTimeInMillis(String givenDateString ){
// String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeInMilliseconds = 0;
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
// System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
return timeInMilliseconds;
}
}
解决方案
通过构造函数在适配器中传递调用活动的上下文,然后使用该上下文:
Context context;
public ConversationsAdapter(Context context,ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler) {
this.context=context;
mConversationOnClickHandler = conversationOnClickHandler;
}
现在在您的适配器中,您可以这样做:
SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "default_value");
推荐阅读
- devstack - 由于错误 401 身份验证,云高仪未升级
- react-native - 什么样的结构在屏幕上使用 Instagram?
- flutter - 似乎可以使用 Flutter 2.0 找到正确的包配置
- python - python中文本文件中第一个单词的字符串
- python - 为什么我的代码查找大数字组合的速度如此之慢?
- arrays - 数组数据不显示
- javascript - 如何使用 JavaScript 访问输入坐标
- c++ - 无法使用线程实现列表操作
- javascript - 使用 javascript 在 textarea 中设置 CKeditor 数据
- python - 使用 firebase_admin 在 python 中使用 firestore 进行身份验证