I have a Volley
call in the onCreate
of my activity. I get the values from my db
ok, phone number strings like 12345
, 23456
Also in my onCreate
I convert these to an Arraylist
and put them in a Sharedpreference
file with:
//we want to bring the checkedContactsAsArrayList array list to our SelectPhoneContactAdapter.
// It looks like Shared Preferences
//only works easily with strings so best way to bring the array list in Shared Preferences is with
//Gson.
//Here, we PUT the arraylist into the sharedPreferences
SharedPreferences sharedPreferencescheckedContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorcheckedContactsAsArrayList = sharedPreferencescheckedContactsAsArrayList.edit();
Gson gsoncheckedContactsAsArrayList = new Gson();
String jsoncheckedContactsAsArrayList = gsoncheckedContactsAsArrayList.toJson(checkedContactsAsArrayList);
editorcheckedContactsAsArrayList.putString("checkedContactsAsArrayList", jsoncheckedContactsAsArrayList);
editorcheckedContactsAsArrayList.commit();
System.out.println("ViewContact: jsoncheckedContactsAsArrayList is " + jsoncheckedContactsAsArrayList);
With System.out.print
the Arraylist
prints correctly, I see 12345
and 23456
, for example.
But when I get the Arraylist
values from Sharedpreferences
in my Custom Adapter
it doesn't show the latest values - it gets the previous instance of the activity, and therefore the wrong checkboxes are checked:
public SelectPhoneContactAdapter(final List<SelectPhoneContact> selectPhoneContacts, Context context, int activity) {
//we are fetching the array list checkedContactsAsArrayList, created in ViewContact.
//with this we will put a tick in the checkboxes of contacts the review is being shared with
SharedPreferences sharedPreferencescheckedContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(_c);
Gson gsoncheckedContactsAsArrayList = new Gson();
String jsoncheckedContactsAsArrayList = sharedPreferencescheckedContactsAsArrayList.getString("checkedContactsAsArrayList", "");
Type type2 = new TypeToken<ArrayList<String>>() {
}.getType();
checkedContactsAsArrayList = gsoncheckedContactsAsArrayList.fromJson(jsoncheckedContactsAsArrayList, type2);
System.out.println("SelectPhoneContactAdapter checkedContactsAsArrayList :" + checkedContactsAsArrayList);
}
Everything else in my getView()
is up to date but System.out.print
shows me the previously checked boxes, like 4567
, 98765
, 23456
, or whatever the previous Arraylist
was, and not 12345
and 23456
, like I should be seeing.
It's like the Custom Adapter
gets the Arraylist
values before they are put into SharedPreferences
file, so therefore it gets the old values and shows the wrong checked checkboxes
. Is this normal Custom Adapter
behaviour? What can I do to ensure the new values are shown?
I've looked at several possible solutions but no fix so far. Could my problem be related to this? (although tried it and couldn't get it to solve my problem):
Android onResume Show Previous state of Activity
Aucun commentaire:
Enregistrer un commentaire