如何从共享首选项中检索数组列表并将数组列表设置为 recyclerview

Posted

技术标签:

【中文标题】如何从共享首选项中检索数组列表并将数组列表设置为 recyclerview【英文标题】:how to retrive array list from shared preferences and set array list to recycler view 【发布时间】:2017-10-19 16:41:37 【问题描述】:

如何从另一个活动中读取 SharedPreferences 值?

我如何从其他活动中读取偏好?

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;
    


【问题讨论】:

你想做什么?你的问题不清楚。 Save ArrayList to SharedPreferences的可能重复 @ritesh please check this link i hope this will work 【参考方案1】:

使用HashSet 将字符串ArrayList 存储到SharedPreferences

这是一个例子:

#.messages 存储到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;

#.SharedPreferences获取messages

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;

#.SharedPreferences 获取ArrayList

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

希望对你有帮助~

【讨论】:

以上是关于如何从共享首选项中检索数组列表并将数组列表设置为 recyclerview的主要内容,如果未能解决你的问题,请参考以下文章

我的共享首选项存储列表视图的数据并检索它不起作用

是否有任何解决方案可以从共享首选项中存储和检索对象列表?

如何在共享首选项上保存 RecyclerView 的 spanCount?

如何保存数组?

如何为第一个操作正确地从数组列表中检索数据

如何从 MongoDB 数组中检索第一个和最后一个元素并将其设置为不同的字段?