Get SharedPreferences string value from adapter

狂风中的少年 提交于 2020-03-05 04:23:28

问题


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.

and now I need to get the value from my Adapter to use it.

I tried many solutions but I get errors.

this is how I store the value:

  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();

    }

}

and this is my Adapter:

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;
}

}


回答1:


In your Adapter, you can retrieve SharedPreferences as follows:

SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "");



回答2:


Pass a context from your adapter constructor and retrieve SharedPreferences instance

public ConversationsAdapter(ConversationsAdapter.ConversationOnClickHandler conversationOnClickHandler, Context context) {
   mConversationOnClickHandler = conversationOnClickHandler;
   pref = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
}



回答3:


Pass the context of the calling activity in your adapter through constructor and then use that context :

Context context;

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

Now in your adapter you can do this:

SharedPreferences preferences = context.getSharedPreferences("log11", Context.MODE_PRIVATE);
String agentId = preferences.getString("agentId", "default_value");


来源:https://stackoverflow.com/questions/57329190/get-sharedpreferences-string-value-from-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!