2014-09-02 46 views
1

如何保存ArrayList以及如何获取它? 我尝试使用SharedPreferences但可能我犯了一些错误!保存并获取ArrayList <Integer> Android

你能帮我解释一些例子吗?

Thanx! :)

+0

您应该使用Sqllite DB,请查看以下链接http://developer.android.com/guide/topics/data/data-storage.html#db – 2014-09-02 18:48:12

回答

0

使用此:

声明这些2个变量:

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

你必须通过一个字符串键和一个ArrayList中,

public boolean setStringSetpref(String key, ArrayList array) { 
     status = true; 
     try { 
      this.array = array; 
      s = new HashSet<String>(array); 
      prefseditor.putStringSet(key, s); 
      prefseditor.apply(); 
     } catch (Exception e) { 
      status = false; 
     } 
     return status; 
    } 

要得到的值,

public ArrayList<String> getStringSetPref(String key) { 
     s = prefs.getStringSet(key, null); 
     array = new ArrayList<String>(s); 
     return array; 
    } 
+0

Thanx your answare is very nice but I need to save a ArrayList ... do你有什么想法我该怎么做? Thanx4你的时间! :) – user2735197 2014-09-02 21:05:52

+0

将整数转换为字符串,并保存它们时,检索从字符串转换为整数....看不到任何其他方式来保存整数数组在sharedpreferences .. – Psypher 2014-09-02 21:10:15

+0

Thanx,但我解决了使用Gson和sharedpreferences! – user2735197 2014-09-03 18:30:31

相关问题