从适配器获取 SharedPreferences 字符串值 [英] Get SharedPreferences string value from adapter
本文介绍了从适配器获取 SharedPreferences 字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在使用 Android Studio 'Java',但对从 SharedPreferences 检索值有疑问.
I'm working now with Android Studio 'Java' and I have question about retrieving value from SharedPreferences.
我在主活动的 SharedPreferences 中存储了字符串值.
I have stored String value in SharedPreferences in the main activity.
现在我需要从我的适配器中获取值才能使用它.
and now I need to get the value from my Adapter to use it.
我尝试了很多解决方案,但出现错误.
I tried many solutions but I get errors.
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;
}
}
推荐答案
在您的 Adapter 中,您可以按如下方式检索 SharedPreferences:
In your Adapter, you can retrieve SharedPreferences as follows:
SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "");
这篇关于从适配器获取 SharedPreferences 字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文