Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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 从SharedReference中删除JSONObject_Java_Android_Arrays_Json - Fatal编程技术网

Java 从SharedReference中删除JSONObject

Java 从SharedReference中删除JSONObject,java,android,arrays,json,Java,Android,Arrays,Json,当我点击storeObjectArray进入SharedReference时,我有一个按钮,现在我需要另一个按钮来删除ObjectArray 如何删除一个条目?我可以从SharedReference中清除所有日期,但这不是我想要的 以下是我要存储的代码: private void logDateTime() { TextView name = (TextView) findViewById(R.id.tv_tel); String m1 = name.getText().to

当我点击store
ObjectArray
进入
SharedReference
时,我有一个按钮,现在我需要另一个按钮来删除
ObjectArray

如何删除一个条目?我可以从SharedReference中清除所有日期,但这不是我想要的

以下是我要存储的代码:

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

}

提前感谢您删除特定保存的首选项,然后使用

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

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

首先尝试获取SharedReference对象

SharedPreferences mySharedPreferences=getSharedPreferences("myStore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("jString1", yourJsonString1);
editor.putString("jString2",yourJsonString2);
editor.commit();
现在是从共享首选项中删除某些内容的时候了。 尝试获取您先前创建的SharedReference对象名
'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
”。您可能希望从
SharedReference
中删除此条目对象。好的,让我们这样做 我假设您已使用
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 
       }  
    }
}
String jsonEntry=mySharedPreferences.getString(“jsonEntry1”,null);
if(jsonEntry!=null){
JSONArray completeArray=新的JSONArray(jsonetry);

对于(int i=0;i您有什么问题?您是否遇到任何错误?我想从SharedReference中删除一个条目…我找不到在您的代码中使用SharedReference。您可以获取最后一个条目的字符数,并可以使用trim方法从json阵列中删除最后一个条目可能重复感谢AndroidUserSiastic为您的重播..我不能现在访问我的工作我会稍后测试我会让你知道的谢谢Tanim reja的重播,你是对的我已经将我的json字符串保存到了一个文件中。但不幸的是我无法成功删除条目logView.setOnLongClickListener(new View.OnLongClickListener())…我正在尝试将其保存在共享引用上。可能会找到更简单的解决方案。我不确定我是否错了?如果您有任何想法从文件中删除json条目,我将不胜感激。我将返回文件。将json保存在共享首选项上太复杂了。啊,您可以反复读写文件…您能尝试读取af json吗把它写进一个文件。那就容易了。但是你对SharedReference的看法是错误的…这并不复杂…它太容易了,然后是文件系统。你需要尝试一下…(我认为你的json结构非常复杂,如果可能的话,你可以重新安排逻辑)