第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如果 arraylist 為空,如何保存到 shredpreferences?

如果 arraylist 為空,如何保存到 shredpreferences?

慕婉清6462132 2022-07-27 20:07:53
每次我的 Android 應(yīng)用程序啟動(dòng)時(shí),我都會(huì)從我的服務(wù)器獲得一個(gè)arraylist, MatchingArrayList:        try {          JSONArray Object = new JSONArray(MatchingAsString);          //for every object in the Array          for (int x = 0; x < Object.length(); x++) {            final JSONObject obj = Object.getJSONObject(x);            MatchingArrayList.add(obj.getString("usernameMatch"));            }它看起來(lái)像這樣:[+111, +222]然后我像arraylist這樣保存sharedpreferences:            SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());            SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();            Gson gsonMAL = new Gson();            String jsonMAL = gsonMAL.toJson(MatchingArrayList);            editorMAL.putString("MatchingArrayList", jsonMAL);            editorMAL.commit();所以下次使用該應(yīng)用時(shí),如果MatchingArrayList已更改為[+111, +222, +333],例如,它將覆蓋上次使用arraylist的[+111, +222]它運(yùn)行良好,除非MatchingArrayList是null或?yàn)榭?。sharedpreferences不更新。我試過(guò)了:          if (MatchingArrayList == null) {            SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());            SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();            editorMAL.putString("MatchingArrayList", null);            editorMAL.commit();          } else {            //save MatchingArrayList into sharedpreferences so we can use it elsewhere            SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());            SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();            Gson gsonMAL = new Gson();            String jsonMAL = gsonMAL.toJson(MatchingArrayList);            editorMAL.putString("MatchingArrayList", jsonMAL);            editorMAL.commit();          }  But still the last arraylist is being used. How can I fix this?
查看完整描述

1 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

我沒(méi)有查看我的 java,arraylist而是檢查了我的應(yīng)用程序一側(cè)response是否為空。php像這樣:


          if (response.isEmpty()) {


            SharedPreferences sharedPreferencesMatchingContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(getApplication());

            SharedPreferences.Editor editorMatchingContactsAsArrayList = sharedPreferencesMatchingContactsAsArrayList.edit();

            editorMatchingContactsAsArrayList.putString("MatchingContactsAsArrayList", "");

            editorMatchingContactsAsArrayList.commit();

          } else {

這解決了我的問(wèn)題,更新了sharedpreferences.


查看完整回答
反對(duì) 回復(fù) 2022-07-27
  • 1 回答
  • 0 關(guān)注
  • 92 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)