我有一个排序的联系人ArrayList
。我想将列表存储在SharedPreference
中。我尝试了以下代码:
SharedPreferences.Editor editor = app_preference.edit();
Set<String> set = new HashSet<String>();
set.addAll(contact_names_list);
editor.putStringSet("CONTACT_LIST", set);
editor.apply();
问题是,当我从HashSet
检索它时,我得到了一个未排序的列表。除了Hashset
之外,还有其他方法可以存储ArrayList
吗?
检索值
Set<String> set = myScores.getStringSet("CONTACT_LIST", null);
static Prefs singleton = null;
public static Prefs with(Context context) {
if (singleton == null) {
singleton = new Builder(context).build();
}
return singleton;
}
这样做-:
SharedPreferences.Editor editor = app_preference.edit();
Set<String> set = new HashSet<String>();
set.addAll(contact_names_list);
editor.putStringSet("CONTACT_LIST", set);
editor.apply();
or
Prefs.with(getAppContext()).putStringSet("CONTACT_LIST", set);
使用以下代码检索值:
Set<String> existindData = Prefs.with(getAppContext()).getStringSet("CONTACT_LIST", null);
因为putStringSet
接受任何实现Set
接口的类 您可以使用保留插入顺序的LinkedHashSet
。
LinkedHashSet的Documentaion:
此实现与 HashSet 的不同之处在于它维护 双向链表贯穿其所有条目。此链接 list 定义迭代顺序,即 元素入到集合中(插入顺序)。
HashSet
不保留插入顺序,这是这里的问题。
// ...
Set<String> set = new LinkedHashSet<String>();
// insert from ArrayList
set.addAll(contact_names_list);
// or single elements
set.add("Homer");
set.add("Marge");
editor.putStringSet("CONTACT_LIST", set);
在共享首选项中保存数组:
SharedPreferences shdPre=getSharedPreferences("MainActivity",MODE_PRIVATE);
shdPre.edit().putInt("Size",myarray.size());
while(i<myarray.size())
{
shdPre.edit().remove("value"+i).commit();
shdPre.edit().putString("value"+i,myarray.get(i)).commit();
i++;
}
从共享首选项加载数组
SharedPreferences shdPre=getSharedPreferences("MainActivity",MODE_PRIVATE);
myarray =new ArrayList();
Int msize=shdPre.getInt("Size",null);
while(i<msize)
{
myarray.add(shdPre.getString("value" + i, null);
i++;
}
正如 Android 文档所解释 https://developer.android.com/training/data-storage/shared-preferences.html?hl=en 共享首选项是一种保存键值数据的方法。鉴于在幕后,该字典几乎可以以随机顺序保存,您无法按顺序检索某些内容。但是,我可以推荐几个解决方案:
- 简单性能不佳的一个:将索引作为值的一部分添加,因此您可以创建一个具有 Hashset 大小的数组,然后迭代 Hashset 并添加您创建的数组中的每个元素。O(N) 时间复杂度加上文件读取过程中涉及的复杂度。
-
将数据保存在本地SQLite数据库中:此解决方案在将来的术语中更加灵活(您可以添加新联系人)。 https://developer.android.com/training/data-storage/sqlite.html 通常,您创建自己的SQLite数据库并将其放入资产中,然后只需导入即可。您可以创建查询以按所需的顺序排列它们(可以为此创建一个列)。
InputStream myInput = context.getAssets( ).open( "test.db" ); // Path to the just created empty db String outFileName = "/data/YOUR_APP/test.db"; // Open the empty db as the output stream OutputStream myOutput = new FileOutputStream( outFileName ); // transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while( ( length = myInput.read( buffer ) ) > 0 ) { myOutput.write( buffer, 0, length ); } // Close the streams myOutput.flush( ); myOutput.close( ); myInput.close( );
-
简单不可扩展的选项:在值
folder
中,您始终可以创建一个strings.xml
文件,您可以在其中将该数据作为string-array
放置。 https://developer.android.com/guide/topics/resources/string-resource.html?hl=en<string-array name="types"> <item>A</item> <item>B and users</item> <item>C</item> <item>D</item> <item>E</item> </string-array>
如果你需要一些更有条理的东西,我已经做了这样的事情(保存为JSON)。
<string-array name="quality_attributes">
<item>{id:1,name:Performance,sub:[{id:2,name:Scalability},{id:3,name:Response_Time}]}</item>
<item>{id:4,name:Security,sub:[{id:5,name:Availability,sub:[{id:6,name:Reliability},{id:7,name:Recovery}]},{id:8,name:Privacy}]}</item>
<item>{id:9,name:Interoperability}</item>
<item>{id:10,name:Maintainability}</item>
<item>{id:12,name:Testability}</item>
<item>{id:13,name:Usability,sub:[{id:14,name:Findability},{id:15,name:Correctness}]}</item>
</string-array>
在我的代码中:
String[] elementsArray = getResources().getStringArray(
R.array.quality_attributes);
for (int i = 0; i < attributesArray.length; i++) {
Gson gson = new Gson();
MyJsonObject obj = gson.fromJson(elementsArray[i],
MyJsonObject.class);
// Do something
}
一般来说,如果你想要一些需要改变的东西,第二个选项是最好的选择。如果您正在寻找简单的东西,第三种选择就足够了。
我们可以用Java 8的排序方式对集合进行排序。Set<String> s = new HashSet<String>();
s.add("Kunal");
s.add("Bhism");
s.add("Work");
s.add("Abdgg");
s.add("Psss");
s.add("Work");
List<Object> l = s.stream().sorted().collect(Collectors.toList());
System.out.println(l);
试试上面的代码,你现在会得到排序后的哈希集。