-1

How to I read SharedPreferences value from another activity?

how to i read Preferences from other activities?

public class PickupGroupListAdaper extends RecyclerView.Adapter<PickupGroupListAdaper.ViewHolder>{
    private ArrayList<GroupListItems> groupListItemsArrayList;

    //Create new views (invoked by the layout manager)
    @Override
    public PickupGroupListAdaper.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Creating a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.pickup_group_list,parent,false);

        //set the view's size, margins, paddings and layout parameters

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    //Replace the contents of a view (invoked by the layout manager
    @Override
    public void onBindViewHolder(PickupGroupListAdaper.ViewHolder holder, int position) {
        // - get element from arraylist at this position
        // - replace the contents of the view with that element

        GroupListItems groupListItems = groupListItemsArrayList.get(position);
        holder.tv_group_player_email.setText(groupListItems.getPlayer_email());
    }

    @Override
    public int getItemCount() {
        return groupListItemsArrayList.size();
    }

    //Provide a reference to the views for each data item
    //Complex data items may need more than one view per item, and
    //you provide access to all the views for a data item in a view holder
    public class ViewHolder extends RecyclerView.ViewHolder {
        //each data item is just a string in this case

        public TextView tv_group_player_email;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_group_player_email = (TextView)itemView.findViewById(R.id.tv_group_player_email);
        }
    }

    public PickupGroupListAdaper(ArrayList<GroupListItems> groupListItemsArrayList) {
        this.groupListItemsArrayList = groupListItemsArrayList;
    }

}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • what you trying to do ? your question is not clear. – Pavan May 19 '17 at 14:01
  • Possible duplicate of [Save ArrayList to SharedPreferences](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) – Pavan May 19 '17 at 14:04
  • @ritesh [please check this link i hope this will work](http://stackoverflow.com/questions/22984696/storing-array-list-object-in-sharedpreferences) – Sagar Jethva May 19 '17 at 14:34

1 Answers1

0

Use HashSet to store string ArrayList into SharedPreferences.

Here is an example:

#. Store messages into SharedPreferences:

public static final String KEY_MESSAGES = "messages";
public static final String SHARED_PREF_NAME = "mypref";

............
.................
public boolean saveKeyMessage(String message) {

    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    // Get existing messages
    Set<String> messages = getMessages();

    // Add new message to existing messages
    messages.add(message);

    // Store messages to SharedPreferences
    editor.putStringSet(KEY_MESSAGES, messages);
    editor.apply();

    return true;
}

#. Get messages from SharedPreferences:

public Set<String> getMessages() {

    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);

    // Get messages
    Set<String> messages = sharedPreferences.getStringSet(KEY_MESSAGES, new HashSet<String>());

    return messages;
}

#. Get ArrayList from SharedPreferences:

ArrayList<String> messageList = new ArrayList<String>(getMessages());

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61