首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从适配器获取SharedPreferences字符串值

从适配器获取SharedPreferences字符串值
EN

Stack Overflow用户
提问于 2019-08-02 15:09:06
回答 3查看 908关注 0票数 0

我现在正在使用Android 'Java‘,我对从SharedPreferences中检索值有疑问。

我在主活动中将字符串值存储在SharedPreferences中。

现在我需要从我的Adapter中获得使用它的值。

我尝试了很多解决方案,但我都犯了错误。

我就是这样存储价值的:

代码语言:javascript
运行
复制
  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();

    }

}

这是我的适配器:

代码语言:javascript
运行
复制
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;
}

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-02 15:27:53

在适配器中,可以按以下方式检索SharedPreferences:

代码语言:javascript
运行
复制
SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "");
票数 1
EN

Stack Overflow用户

发布于 2019-08-02 15:34:31

从适配器构造函数传递上下文并检索SharedPreferences实例

代码语言:javascript
运行
复制
public ConversationsAdapter(ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler, Context context) {
   mConversationOnClickHandler = conversationOnClickHandler;
   pref = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
}
票数 1
EN

Stack Overflow用户

发布于 2019-08-02 15:42:12

通过构造函数传递适配器中调用活动的上下文,然后使用该上下文:

代码语言:javascript
运行
复制
Context context;

public ConversationsAdapter(Context context,ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler) {
   this.context=context;
   mConversationOnClickHandler = conversationOnClickHandler;
}

现在,在适配器中您可以这样做:

代码语言:javascript
运行
复制
SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "default_value");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57329190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档