1

同じキーを使用して、Android アプリの共有設定に複数の値を保存する方法がよくわかりません。

アプリが行うことは、その項目をお気に入りに追加するためのリストとボタンを表示することです。その数値を設定に保存し、ユーザーがお気に入りリストに移動すると、格納されているすべてのお気に入りを HTTP ポスト経由で配列に送信します。

EDIT:私はこのようにやっていますが、新しい値を追加すると最後の値が上書きされます.implodeメソッドをチェックして、新しい値を空の保存リストに追加するか、新しい値を追加して最後の値を保持する必要があります.

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class FavoriteActivity {

    private static String FAVORITE_LIST = "FAV_LIST";
    private static SharedPreferences sharedPreference;
    private static Editor sharedPrefEditor;

    public static List<NameValuePair> getFavoriteList(Context context) {
        if (sharedPreference == null) {
            sharedPreference = PreferenceManager
                    .getDefaultSharedPreferences(context);
        }
        String storedList = sharedPreference.getString(FAVORITE_LIST, "");
        return explode(storedList);
    }

    public static void saveFavorite(String fav, Context context) {
        if (sharedPreference == null) {
            sharedPreference = PreferenceManager
                    .getDefaultSharedPreferences(context);
        }
        sharedPrefEditor = sharedPreference.edit();
        implode(getFavoriteList(context), fav, 1);
        sharedPrefEditor.putString(FAVORITE_LIST, fav);
        sharedPrefEditor.commit();
    }

    public static List<NameValuePair> explode(String string) {
        StringTokenizer st = new StringTokenizer(string, ",");
        List<NameValuePair> v = new ArrayList<NameValuePair>();
        for (; st.hasMoreTokens();) {
            v.add(new BasicNameValuePair("id[]", st.nextToken()));
        }
        return v;
    }

    public static String implode(List<NameValuePair> list, String value,
            int mode) {
        StringBuffer out = new StringBuffer();
        switch (mode) {
        case 0:
            list.remove(new BasicNameValuePair("id[]", value));
            break;
        case 1:
            list.add(new BasicNameValuePair("id[]", value));
            break;
        }
        boolean first = true;
        for (NameValuePair v : list) {
            if (first)
                first = false;
            else
                out.append(",");
            out.append(v.getValue());
        }
        return out.toString();
    }

}
4

3 に答える 3

3

これはできません。どこでも同じキーを使用すると、以前の値が上書きされます。

おそらく、値を配列に変換して配列を保存するか、SQLite データベースの使用を検討して、ある列にキーを指定し、対応する値を別の列に指定してから、すべての行を選択する SELECT ステートメントを実行することができます。その中に鍵付き。

于 2013-04-05T17:34:11.347 に答える
0

OK、これは私が行った方法であり、完璧に機能します。また、重複を追加しません。

private static String FAVORITE_LIST = "FAV_LIST";
private static SharedPreferences sharedPreference;
private static Editor sharedPrefEditor;

public static List<NameValuePair> getFavoriteList(Context context) {
    if (sharedPreference == null) {
        sharedPreference = PreferenceManager
                .getDefaultSharedPreferences(context);
    }
    String storedList = sharedPreference.getString(FAVORITE_LIST, "");
    return explode(storedList);
}

public static void saveFavorite(String fav, Context context) {
    modifyFavorite(fav, context, 1);
}

public static void removeFavorite(String fav, Context context) {
    modifyFavorite(fav, context, 0);
}

private static void modifyFavorite(String fav, Context context, int mode) {
    if (sharedPreference == null) {
        sharedPreference = PreferenceManager
                .getDefaultSharedPreferences(context);
    }
    sharedPrefEditor = sharedPreference.edit();
    String newList = implode(getFavoriteList(context), fav, mode);
    sharedPrefEditor.putString(FAVORITE_LIST, newList);
    sharedPrefEditor.commit();
}

private static List<NameValuePair> explode(String string) {
    StringTokenizer st = new StringTokenizer(string, ",");
    List<NameValuePair> v = new ArrayList<NameValuePair>();
    for (; st.hasMoreTokens();) {
        v.add(new BasicNameValuePair("id[]", st.nextToken()));
    }
    return v;
}

private static String implode(List<NameValuePair> list, String value,
        int mode) {
    StringBuffer out = new StringBuffer();
    switch (mode) {
    case 0:
        list.remove(new BasicNameValuePair("id[]", value));
        break;
    case 1:
        list.add(new BasicNameValuePair("id[]", value));
        break;
    }
    boolean first = true;
    for (NameValuePair v : list) {
        if (out.lastIndexOf(v.getValue()) == -1) {
            if (first) {
                first = false;
            } else {
                out.append(",");
            }
            out.append(v.getValue());
        }
    }
    return out.toString();
}
于 2013-04-05T19:43:43.540 に答える
0

API 11以降、StringSetを置くことができます。別の方法は不可能です。

于 2013-04-05T17:38:21.570 に答える