ArrayList<String>() and SharedPreference using ObjectSerializer

559 views Asked by At

I have an application that saves an array list of String and serializes it using an ObjectSerializer then saves that serialized list in a SharedPreferences instance.

It involves two activities "putting" and "getting" from the same SharePreferences object.

In a ListActivity, I long-click an item to start a deletion process. After the String deletes, I re-initialize the ListAdapter with the updated ArrayList<String> object to update the ListView. After I restart the ListAcivity by calling it again, the item REAPPEARS in my list. It causes a NullPointerException when clicked because it now points to an object that doesnt exist anymore (null).

I have tried removing items from the ArrayList<String> and then putting it in the ObjectSerializer then SharedPreferences. I even tried removing the old SharedPreference value (using clear() or remove() methods). Maybe I'm just overlooking a step, but I cannot figure out what can be causing it.

Would anyone be able to show or walk me through how to serialize an ArrayList<String>, save to the SharedPreference object, remove and item from the list, and update the SharedPreference object so I can get the same (correctly updated list) SharedPreference object from another activity?

Thank you so much!

Below is my (shortened) code:

Activity 1:

public static final String TITLE_PREF = "titleList";
ArrayList<String> titleList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveSongFile(title, song);
        }
    });
}

private void saveSongFile(String title, String song) {
    BufferedWriter bufferWriter = null;
    try {
        FileOutputStream fos = openFileOutput(title, Context.MODE_PRIVATE);
        bufferWriter = new BufferedWriter(new OutputStreamWriter(fos));
        bufferWriter.write(song);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bufferWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // new songs (not updated songs) go to the top
    if (!titleList.contains(title))
        titleList.add(0, title);
    mSetTitleListAdapter(titleList);
    saveTitleArray();
}

private void saveTitleArray() {
    prefEditor = titlePref.edit();
    try {
        prefEditor.putString(TITLE_PREF, ObjectSerializer.serialize(titleList));
    } catch (IOException e) {
        e.printStackTrace();
    }
    prefEditor.apply();
}

private void mSetTitleListAdapter(ArrayList<String> List) {
    autoCompleteAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_1,
            List
    );
    titleBox.setAdapter(autoCompleteAdapter);
}

Activity 2 (ListActivity):

public static final String EXTRA_NAME = "com.frankbrenyah.lyricmaker.NAME";
public static final String TITLE_PREF = "titleList";    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_song_list);

    //init string array from blank list or sharedPref saved data
    songListArray = new ArrayList<>();
    titlePref = getSharedPreferences(TITLE_PREF, MODE_PRIVATE);
    try {
        songListArray = (ArrayList<String>) ObjectSerializer.deserialize(
                titlePref.getString(TITLE_PREF,
                        ObjectSerializer.serialize(new ArrayList<String>())
                )
        );
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    mSetListAdapter(songListArray);

    getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            final AlertDialog.Builder confirmDel = new AlertDialog.Builder(pickSong_List.this);
            confirmDel.setTitle("Delete?")
                    .setMessage("This cannot be undone!")
                    .setPositiveButton("DO NOT delete", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //system default is dismiss()
                        }
                    })
                    .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //delete song, then update title list
                            getApplicationContext().deleteFile(songListArray.get(position));
                            songListArray.remove(position);
                            updateSharedPref(titlePref, TITLE_PREF, songListArray);
                            mSetListAdapter(songListArray);
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Deleted",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            confirmDel.create().show();
            return true;
        }
    });
}

@Override
protected void onListItemClick (ListView l, View v, int position, long id){
    //return selected song to main activity so it can be loaded into text box's
    Intent resultIntent = new Intent(EXTRA_NAME, Uri.parse(songListArray.get(position)));
    setResult(RESULT_OK, resultIntent);
    finish();
}

private void mSetListAdapter(ArrayList<String> list) {
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_1,
            list
    );
    setListAdapter(arrayAdapter);
}

private void updateSharedPref(SharedPreferences sharedPref,
                              String prefFileName,
                              ArrayList<String> list) {

    SharedPreferences.Editor editor = sharedPref.edit();
    try {
        editor.putString(prefFileName, ObjectSerializer.serialize(list));
    } catch (IOException e) {
        e.printStackTrace();
    }
    editor.apply();
}

ADDITION: Below is one error instance from a randomly selected user.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,
  request=0, result=-1, data=Intent { act=com.my.app.NAME
  dat=Scrivo una Canzone }} to activity
  {com.my.app/com.my.app.ActivityONE}:
  java.lang.NullPointerException: Attempt to invoke virtual method 'void
  java.io.BufferedReader.close()' on a null object reference
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3633)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3676)
  at android.app.ActivityThread.access$1300(ActivityThread.java:151)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5351)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  ... 10 more
0

There are 0 answers