【问题标题】:Remove JSONObject from Sharedpreference从 Sharedpreference 中删除 JSONObject
【发布时间】:2016-07-22 21:20:46
【问题描述】:

当我点击商店ObjectArray 进入sharedPreference 时,我有按钮,现在我想要另一个按钮来删除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();
    }

}

提前致谢

【问题讨论】:

  • 你的问题是什么?你遇到什么错误了吗?
  • 我想从 sharedPreference 中删除一个条目...
  • 我无法在您的代码中使用 SharedPreferences
  • 你可以得到最后一个条目的字符数,并可以使用 trim 方法从 json 数组中删除最后一个条目

标签: java android arrays json


【解决方案1】:

我在您的代码中没有看到任何关于共享首选项的信息。您已将 json 字符串保存到文件中。尽管您可以像这样将此 json 字符串放入共享首选项中

首先尝试获取一个 sharedpreference 对象

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

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

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

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

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 将成为“入口”免费 arrayObject,您可以再次将其放入 sharedpreference ..

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

【讨论】:

  • 感谢 Tanim reja 的重播,你说得对,我已将我的 json 字符串保存到文件中。但不幸的是,我无法成功删除条目 logView.setOnLongClickListener(new View.OnLongClickListener() .. 我正在尝试将其保存在 sharedprefrance 上。可能更容易找到解决方案。我不确定我是错还是对?
  • 如果您有任何想法从文件中的 json 中删除条目,我将不胜感激。我会回去归档。在共享偏好上保存 json 太复杂了
  • 是的,您可以一次又一次地读取和写入文件...您可以在将其写入文件后尝试读取该 json.. 那么这很容易。但是您对 SharedPreference 的看法是错误的……这并不复杂……它对文件系统来说太简单了。您需要尝试一下..(我认为您的 json 结构非常复杂,如果可能,您可以重新安排逻辑)
【解决方案2】:

要删除特定保存的首选项,然后使用

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();

【讨论】:

  • 感谢 AndroidEnhusiastic 的重播。我现在无法访问我的工作,稍后我会测试它,我会通知你
猜你喜欢
  • 2014-07-30
  • 2016-03-09
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 2017-12-21
相关资源
最近更新 更多