2015-06-23 38 views
0

我已經寫了一些代碼來保存新添加的項目到sharedpreferences。但是,如果我在數組列表中檢索sharedpreferences,它會顯示{"todo":"new item" },但如果您瞭解我,我只想顯示一個項目和僅顯示文本。這是我保存Arraylist代碼:android java在ArrayList中檢索sharedpreferences

public void saveArrayList(Context context ,ArrayList<DayItems> mlist){ 
    SharedPreferences shared; 
    SharedPreferences.Editor editor; 

    shared = context.getSharedPreferences("MONDAY", Context.MODE_PRIVATE); 
    editor = shared.edit(); 

    Gson gson = new Gson(); 
    String json = gson.toJson(mlist); 
    editor.putString("mondAy", json); 
    String getSaved = shared.getString("mondAy", null); 
    Toast.makeText(PlannerActivity.this, getSaved, Toast.LENGTH_LONG).show(); 
    editor.commit(); 
} 

這裏是我的附加項目的ArrayList:

@Override 
           public void onClick(View v) { 
            String afspraak = addTEXT.getText().toString().trim(); 
            if(afspraak.length()>0){ 
             addTEXT.setText(""); 
             mon.add(new DayItems(afspraak)); 
             dayAdapter.notifyDataSetChanged(); 
             //save arraylist with new item 
             saveArrayList(context, mon); 
            } 

           } 
          }); 

其實我要的是檢索SharedPreferences片段。 在此先感謝!

回答

1

你可以試試這個從SharedPreferences

List<DayItems> listDayItems = gson.fromJson(shared.getString("mondAy", null), new TypeToken<List<DayItems>>() { 
     }.getType()); 
+0

由於這一個工程! – svenvdz

0

這肯定與你的工作條件檢索:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); 
Gson gson = new Gson(); 

String json = sharedPrefs.getString(TAG, null); 

Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType(); 

ArrayList<ArrayObject> arrayList = gson.fromJson(json, type); 
+0

謝謝你的回答!我一定會試試這個! – svenvdz

+0

謝謝@svenvdz快速響應,因爲我們給你回覆你的帖子。再次感謝 –