Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将阵列保存到内部设备_Java_Android_Arrays_Outputstream - Fatal编程技术网

Java 将阵列保存到内部设备

Java 将阵列保存到内部设备,java,android,arrays,outputstream,Java,Android,Arrays,Outputstream,为了找到保存4个String[]数组的简单方法,我已经搜索了好几周了。我试着将它们转换成一个集合并保存到SharedRef,但效果似乎不太好。我也遇到过outputStream,但我没有找到任何好的例子 我想保存以下4件物品: String[] debtName = new String[10]; String[] debtAmount = new String[10]; String[] debtRate = new String[10]; String[] debtPayment = new

为了找到保存4个String[]数组的简单方法,我已经搜索了好几周了。我试着将它们转换成一个集合并保存到SharedRef,但效果似乎不太好。我也遇到过outputStream,但我没有找到任何好的例子

我想保存以下4件物品:

String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
信息正通过Intents从另一个传递到此活动


请提供任何简单的建议,以便在内部保存这些数组。

您可以通过将这些数组转换为具有逗号分隔值的字符串来保存,并将其保存在共享首选项中。 比如:

使用可以将数据保存为JSON数组

public class JSONSharedPreferences {
    private static final String PREFIX = "json";

    public static void saveJSONObject(Context c, String prefName, String key, JSONObject object) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(JSONSharedPreferences.PREFIX+key, object.toString());
        editor.commit();
    }

    public static void saveJSONArray(Context c, String prefName, String key, JSONArray array) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(JSONSharedPreferences.PREFIX+key, array.toString());
        editor.commit();
    }

    public static JSONObject loadJSONObject(Context c, String prefName, String key) throws JSONException {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        return new JSONObject(settings.getString(JSONSharedPreferences.PREFIX+key, "{}"));
    }

    public static JSONArray loadJSONArray(Context c, String prefName, String key) throws JSONException {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        return new JSONArray(settings.getString(JSONSharedPreferences.PREFIX+key, "[]"));
    }

    public static void remove(Context c, String prefName, String key) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        if (settings.contains(JSONSharedPreferences.PREFIX+key)) {
            SharedPreferences.Editor editor = settings.edit();
            editor.remove(JSONSharedPreferences.PREFIX+key);
            editor.commit();
        }
    }

}

我不太明白你的问题。。。为什么不通过intent bundle extra发送这些值呢?其中一种方法是将数组设置为逗号分隔的值(
String
)。这样,您就可以将其保存到
SharedReferences
作为
String
。当您想将其用作数组时,只需将其拆分为
split()
String[])@antimo即可-您能举个例子吗?假设它能工作,我会帮你检查。你为什么不使用SQLite?不熟悉。。。我还没有学会如何做-如果您愿意为它抛出一个答案,我将看看它是如何工作的。GetDefaultSharedReferences(getContext()).getString(key“”)这部分为“方法getContext()抛出一个错误对于类型DebtList也未定义-我如何调用GetFromSharedReference方法?我已经完成了“GetFromSharedReference(key)”;“但是我得到的错误是“null指针异常”,而不是getContext()用户您的上下文。我还有一个知识问题,这样我就可以了解更多我在这里做的事情……在getFrom方法上,您有一个返回值;语句……我理解返回将(因为没有更好的词)终止该方法并将您返回到之前的位置;但是,我不明白的是变量…值会发生什么情况?如果我想返回debtName[]数组,我需要在onCreate中调用该方法的位置做什么?如果是“debtName”。要保存到首选项调用:puttoSharedRef(debtName,“debtName”)“,上下文)。要得到同样的结果,请调用:GetFromSharedReference(“debtName”,context)。今晚我将尝试此操作,并让您知道它是如何工作的。感谢您的精彩回复。我如何将其加载回各个数组中;jArray=JSONSharedPreferences.loadJSONArray(this.getApplicationContext(),“prefName”,“prefKey”);字符串jsonString=jArray.toString();jsonString.replace(“},{,“,”);String[]数组=jsonString.split(“”);第一次运行它时,它返回“[]”;第二次运行它时,它返回“[]”-有什么建议吗?
JSONArray mJSONArray = new JSONArray(Arrays.asList(debtName));

JSONSharedPreferences.saveJSONArray(this.getApplicationContext(), "prefName", "prefKey", mJSONArray);
public class JSONSharedPreferences {
    private static final String PREFIX = "json";

    public static void saveJSONObject(Context c, String prefName, String key, JSONObject object) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(JSONSharedPreferences.PREFIX+key, object.toString());
        editor.commit();
    }

    public static void saveJSONArray(Context c, String prefName, String key, JSONArray array) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(JSONSharedPreferences.PREFIX+key, array.toString());
        editor.commit();
    }

    public static JSONObject loadJSONObject(Context c, String prefName, String key) throws JSONException {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        return new JSONObject(settings.getString(JSONSharedPreferences.PREFIX+key, "{}"));
    }

    public static JSONArray loadJSONArray(Context c, String prefName, String key) throws JSONException {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        return new JSONArray(settings.getString(JSONSharedPreferences.PREFIX+key, "[]"));
    }

    public static void remove(Context c, String prefName, String key) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        if (settings.contains(JSONSharedPreferences.PREFIX+key)) {
            SharedPreferences.Editor editor = settings.edit();
            editor.remove(JSONSharedPreferences.PREFIX+key);
            editor.commit();
        }
    }

}