简体   繁体   中英

can't get data from SharedPreferences

First of all I call getFriends . Using Request I get data. It work correctly and gives not null

getFriends(getActivity());

 private void getFriends(final Context context){
    VKRequest request = VKApi.friends().get(VKParameters.from(VKApiConst.FIELDS,
            "id,first_name,last_name,photo_100,"));
    request.executeWithListener(new VKRequest.VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            super.onComplete(response);
            VKList<VKApiUser> Friends= (VKList<VKApiUser>)response.parsedModel;

            SharedPreferences prefs  = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();

            Toast.makeText(context,Friends.get(0).last_name,Toast.LENGTH_LONG).show();//NOT NULL

            for(int i = 0; i< Friends.size();++i) {
                editor.putString("FriendFirstName" + String.valueOf(i), "asdsaddsa");
                editor.putString("FriendLastName"+ String.valueOf(i), Friends.get(i).last_name);
                editor.putString("FriendPhoto"+ String.valueOf(i), Friends.get(i).photo_100);
            }
        }
    });
}

Then I call LoadData and try to get data from it

LoadFriends(getActivity());



 public void LoadFriends(Context context){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        for(int i=0;i< 7; i++) {
            if (prefs != null) {
                Names.add(prefs.getString("FriendFirstName" + String.valueOf(i), null)
                        + " " + prefs.getString("FriendLastName" + String.valueOf(i), null));
            }
        }
}

And it puts me data that was previoud time, I mean it doesn't give me corrent data; What am I doing wrong?

您需要在SharedPreferences.Editor上调用commit()apply() ,以便更改得以SharedPreferences.Editor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM