从Sharedpreference

时间:2016-04-03 09:53:06

标签: java android arrays json

点击商店ObjectArraysharedPreference时,我有按钮,现在我想要另一个按钮删除ObjectArray

如何删除一个条目?我可以设法清除sharedPreference中的所有日期,但它不是我想要的......

这是我存储的代码:

 private void logDateTime() {

    TextView name = (TextView) findViewById(R.id.tv_tel);
    String m1 = name.getText().toString();
    mTvName = (TextView) findViewById(R.id.tv_name);
    Intent i = new Intent(CountryActivity1.this, LogActivity.class);
    String m2 = mTvName.getText().toString();
    startActivity(i);

    // Variables
    String date = DateFormat.getDateInstance().format(new Date());
    String time = DateFormat.getTimeInstance().format(new Date());
    String desc = m2;
    String desc1 = m1;
    JSONArray completeArray = new JSONArray();

    try {
        // Open the JSON file and initialize a string builder
        FileInputStream in = openFileInput(FILENAME);
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader bufferedReader = new BufferedReader(
                inputStreamReader);
        StringBuilder sb = new StringBuilder();
        String line;
        // Read the existing content in the JSON file and add it to the
        // string builder
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }

        if (sb.toString() != "") {
            JSONArray temp_arr = new JSONArray(sb.toString());
            completeArray = temp_arr;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    catch (JSONException e) {
        e.printStackTrace();
    }

    // Initialize the JSON object for the new entry
    JSONObject entry = new JSONObject();
    // Initialize the JSON object that will contain the entry object
    JSONObject finalEntry = new JSONObject();

    try {
        // Add the time and date to the entry object
        entry.put("date", date);
        entry.put("time", time);
        entry.put("description", desc);
        entry.put("description1", desc1);
        // Add the entry object to a new object called "entry"
        finalEntry.put("entry", entry);

        completeArray.put(finalEntry);

        // Convert the complete array in to a string
        String jsonEntry = completeArray.toString();

        // Write complete array to the file
        FileOutputStream fos = openFileOutput(FILENAME,
                Context.MODE_PRIVATE);
        fos.write(jsonEntry.getBytes());
        fos.close();
        // Notify that an entry has been created
        Toast toast = Toast.makeText(getApplicationContext(), "Saved",
                Toast.LENGTH_LONG);
        toast.show();
    }

    catch (JSONException e) {
        e.printStackTrace();
    }

    catch (IOException e) {
        e.printStackTrace();
    }

}

提前致谢

2 个答案:

答案 0 :(得分:0)

要删除特定的已保存首选项,请使用

SharedPreferences.Editor editor = settings.edit();
editor.remove("tag_to_delete");
editor.commit();

(或)

使用remove方法可能是正确的方法。

mPrefs = PlaniActivity.this.getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
prefsEditor.remove("list");

您也只需要一个编辑器,所以只需要删除项目

prefsEditor.remove("platamatBeansArrayList");

您只需要在最后应用或提交一次,因为所有事件都排队等待。

prefsEditor.apply();

答案 1 :(得分:0)

我在代码中没有看到任何关于共享偏好的内容。您已将json字符串保存到文件中..虽然您可以将此json字符串以这种方式放入共享首选项

首先尝试获取共享首选项对象

SharedPreferences mySharedPreferences=getSharedPreferences("myStore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("jString1", yourJsonString1);
editor.putString("jString2",yourJsonString2);
editor.commit();

现在是时候从共享偏好中删除某些内容了。 尝试获取您之前创建的共享首选项对象名称'myStore'

SharedPreferences mySharedPreferences=getSharedPreferences("myStore", Context.MODE_PRIVATE);
if(sharedPreferences != null) {
   SharedPreferences.Editor editor = mySharedPreferences.edit();
   editor.remove("jString1");
   editor.commit();
}

以上是正常程序.. 但我发现你的代码中有些棘手。您的jsonEntryjsonString,其中包含JsonArray (completeArray),此数组包含另一个jsonObject' finalEntry'最后这个' finalEntry'有一个' entry' jsonObject。可能是您要从sharedPreference中删除此条目对象。哦,让我们这样做 我假设您已将jsonEntry存储在jsonEntry1密钥sp中。尝试检索该值。

String jsonEntry =mySharedPreferences.getString("jsonEntry1",null);
if(jsonEntry != null){
    JSONArray completeArray = new JSONArray(jsonEntry);
    for (int i=0;i<completeArray .length();i++){
     JSONObject finalEntry=(JSONObject) completeArray .get(i);
       if(finalEntry.has("entry")){
        finalEntry.remove("entry");// for remove only one entry object you need to add your own logic 
       }  
    }
}

完成循环后,您的completeArray将成为&#39;条目&#39; free arrayObject,你可以把它再次放到sharedpreference ..

希望它有效。 (可能需要自定义此逻辑)