我现在正在使用Android 'Java‘,我对从SharedPreferences中检索值有疑问。
我在主活动中将字符串值存储在SharedPreferences中。
现在我需要从我的Adapter中获得使用它的值。
我尝试了很多解决方案,但我都犯了错误。
我就是这样存储价值的:
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;
}}
发布于 2019-08-02 15:27:53
在适配器中,可以按以下方式检索SharedPreferences:
SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "");发布于 2019-08-02 15:34:31
从适配器构造函数传递上下文并检索SharedPreferences实例
public ConversationsAdapter(ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler, Context context) {
mConversationOnClickHandler = conversationOnClickHandler;
pref = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
}发布于 2019-08-02 15:42:12
通过构造函数传递适配器中调用活动的上下文,然后使用该上下文:
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");https://stackoverflow.com/questions/57329190
复制相似问题