これは私の最初のスタックオーバーフローの質問です。私はこれについて多くのグーグルをしました。Hashsets、Treesets、LinkedHashSets、Collections、Stacks (Stack クラスは非推奨ですか?)... SQLite を使用することもできますが、当面はそれを避けようとしています。
Android Studio でアプリを作成しています。このアプリは人々を扱い、それらをリストし、さまざまな方法で連絡します。アプリのユーザーは、最近連絡したリスト、ブロックされたリスト、お気に入りの 3 種類のリストを維持および管理できます。これらのリストは、共有設定に文字列セットとして保存されるため、アプリを閉じて再度開いても存続します。個々の文字列は、オンライン データベースを使用してリストが作成されるときに主キーとして機能します。このリストの順序が重要であるため、「最近連絡した」リストに最も関心があります。
問題は、私が理解している限り、文字列セットを宣言すると次のようになることです。
Set<String> faveArray = new LinkedHashSet<String>;
私が理解しているように、右側では HashSet、LinkedHashSet、または TreeSet しか使用できません。
私は LinkedHashset を使用していたので、アプリを再度開いて共有設定からデータを取り出すと、最近追加されたもの (LIFO / スタック) から文字列が取り出されることを期待していたので、リストビューに入力すると、それらは、リストの一番上にある「最近連絡した」人などとともに表示されます...または、少なくとも、私が作業できるある種の予測可能な順序/動作があることを期待していました.
ということで....... 共有設定 I/O クラスのコードを添付しました。
私のメインアプリから、私は次のようなことをします:
static SharedPrefUTIL sharedPrefUTIL;
sharedPrefUTIL = new SharedPrefUTIL(this);
sharedPrefUTIL.addRecent("derp");
sharedPrefUTIL.addRecent("nerp");
sharedPrefUTIL.addRecent("gerp");
sharedPrefUTIL.addRecent("herp");
この時点で、コード
Log.i(TAG, set + " committed to " + key);
クラス SharedPrefUTIL Logs の commit() メソッド:
" [derp, nerp, gerp, herp] recentArray にコミットされました "
以下を実行すると、アプリを閉じて再度開いた後にバーが発生します。
sharedPrefUTIL.toastContents("R");
結果は一見ランダムな順序です。<<<< それが私の問題です。
どんな助けでも大歓迎です。
package com.secretsoft.booberbunz;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
/**
* Created by Programming on 2/22/2016.
*/
public class SharedPrefUTIL {
protected static final String TAG = "CXX SharedPrefUTIL";
Context context;
SharedPreferences sharedPreferences;
Set<String> faveArray;
Set<String> blockArray;
Set<String> recentArray;
public static final Set<String> DEFAULT = new HashSet<String>(Arrays.asList("empty"));
public SharedPrefUTIL(Context context){
this.context = context;
// load shared prefs into static arrays (new objects to prevent problems)
sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
recentArray = new LinkedHashSet<String>(sharedPreferences.getStringSet("recentArray",DEFAULT));
blockArray = new LinkedHashSet<String>(sharedPreferences.getStringSet("blockArray",DEFAULT));
faveArray = new LinkedHashSet<String>(sharedPreferences.getStringSet("faveArray",DEFAULT));
Log.i(TAG, "SharedPrefUTIL instance created");
if (recentArray.contains("empty")) {
recentArray.clear();
Log.i(TAG, "recentArray contains the string -empty- and was cleared");
}
if (blockArray.contains("empty")) {
blockArray.clear();
Log.i(TAG, "blockArray contains the string -empty- and was cleared");
}
if (faveArray.contains("empty")) {
faveArray.clear();
Log.i(TAG, "faveArray contains the string -empty- and was cleared");
}
}
public void toastLength(String type){
if (type == "R"){
String temp = type + " array is this long: " + recentArray.size();
Toast.makeText(context, temp,
Toast.LENGTH_LONG).show();
Log.i(TAG, temp);
}
else if (type == "B"){
String temp = type + " array is this long: " + blockArray.size();
Toast.makeText(context, temp,
Toast.LENGTH_LONG).show();
Log.i(TAG, temp);
}
else if (type == "F"){
String temp = type + " array is this long: " + faveArray.size();
Toast.makeText(context, temp,
Toast.LENGTH_LONG).show();
Log.i(TAG, temp);
}
else {
Log.i(TAG, "invalid type param given to toastLength()");
}
}
public void toastContents(String type){
if (type == "R"){
for (String temp : recentArray) {
Toast.makeText(context, temp,
Toast.LENGTH_LONG).show();
Log.i(TAG, "recentArray contains: " + temp);
}
}
else if (type == "B"){
for (String temp : blockArray) {
Toast.makeText(context, temp,
Toast.LENGTH_LONG).show();
Log.i(TAG, "blockArray contains: " + temp);
}
}
else if (type == "F"){
for (String temp : faveArray) {
Toast.makeText(context, temp,
Toast.LENGTH_LONG).show();
Log.i(TAG, "faveArray contains: " + temp);
}
}
else {
Log.i(TAG, "invalid type param given to toastContents()");
}
}
public void clearList(String type){
if (type == "R"){
recentArray.clear();
commit("recentArray", recentArray);
Toast.makeText(context,"recent list has been cleared.", Toast.LENGTH_LONG);
}
else if (type == "B"){
blockArray.clear();
commit("blockArray", blockArray);
Toast.makeText(context,"blacklist has been cleared.", Toast.LENGTH_LONG);
}
else if (type == "F"){
faveArray.clear();
commit("faveArray", faveArray);
Toast.makeText(context,"favorites have been cleared.", Toast.LENGTH_LONG);
}
else {
Log.i(TAG, "invalid type param given to clearList()");
}
}
public void addRecent(String newRecent){
recentArray.add(newRecent);
commit("recentArray", recentArray);
Log.i(TAG, newRecent + " added to recentArray");
}
public void addBlocked(String newBlocked, String nick){
blockArray.add(newBlocked);
commit("blockArray", blockArray);
Toast.makeText(context, nick + " has been blacklisted!", Toast.LENGTH_SHORT);
}
public void remBlocked(String remBlocked, String nick){
blockArray.remove(remBlocked);
commit("blockArray", blockArray);
Toast.makeText(context, nick + " has been unblocked.", Toast.LENGTH_SHORT);
}
public void addFave(String newFave, String nick){
faveArray.add(newFave);
commit("faveArray", faveArray);
Toast.makeText(context, nick + " added to favorites!", Toast.LENGTH_SHORT);
}
public void remFave(String remFave, String nick){
faveArray.remove(remFave);
commit("faveArray", faveArray);
Toast.makeText(context, nick + " removed from favorites.", Toast.LENGTH_SHORT);
}
public void commit(String key, Set<String> set){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet(key,set);
editor.commit();
Log.i(TAG, set + " committed to " + key);
}
}