【问题标题】:how to retrive array list from shared preferences and set array list to recycler view如何从共享首选项中检索数组列表并将数组列表设置为 recyclerview
【发布时间】: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;
    }

}

【问题讨论】:

标签: android arrays list android-recyclerview sharedpreferences


【解决方案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());

希望对你有帮助~

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多