Search code examples
javaandroidsharedpreferencessettext

Using SharedPreferences only setText() works but append() does not


I have a main activity which has two fragments and I am trying to pass some data which I want to append on above whatever texts is already on a edittext on the next fragment.

Activity with two separate Tabs:

enter image description here

The following works fine:

Fragment #1:

String y = "TEST 1";
SharedPreferences prefs;  // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit();
editor.putString("someId", y);
editor.commit();

Fragment #2:

SharedPreferences prefs;  // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
String someId=prefs.getString("someId","");
showLog.setText(someId + "\n HERE"); //this overwrites the text and is multiline

What I am looking to do is I want the showLog to append above what's already there.

My showLog is the following:

        <EditText
            android:id="@+id/showLog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Nothing to display"
            android:inputType="textMultiLine"
            android:lines="12"
            android:paddingLeft="2dip"
            android:singleLine="false"
            android:textColor="#999999"
            android:textSize="14dip"
            android:textStyle="normal"
            android:gravity="top" />

For example:

showLog already starts out with having "THIS IS A TEST" in the textbox When the SharedPreference is called, the showLog should display the following:

TEST 1
 HERE
THIS IS A TEST

But that's not happening. I tried using .append() which didn't have any affect.


Solution

  • I think I understand what you're trying to do now, you might want to try:

    int start = showLog.getSelectionStart();
    int end = showLog.getSelectionEnd();
    String toIns = someId + "\n HERE";
    showLog.getText().replace(Math.min(start, end), Math.max(start, end), toIns, 0, toIns.length());
    

    Technically it's not appending, rather replacing the end of the string with new text.

    EDIT: in light of the new issue arising, here are my edits to your project, let me know if there's anything wrong still.

    My edited version is on the right, original on the left

    CurrentTrip.java

    DisplayTrip.java

    What I'm doing with the text might not be exactly what you want, so make sure to let me know if it's not.

    EDIT 2: And to delete the stored values:

    final SharedPreferences.Editor editor = prefs.edit();
    editor.clear();
    editor.commit();
    

    EDIT 3: Having understood exactly what it is you want to do, here's one way to do it by storing the last trip added, and keeping track of the text you need in your TextView.

    prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
    // Find the string we want
    String someId = prefs.getString("someId","");
    final Editor editor = prefs.edit();
    // To stop a trip being added in onResume etc.
    if(someId != prefs.getString("previous-trip", "")){
        showLog.setText(someId + prefs.getString("previous", ""));
    } else {
        // Without this else, we'd have a blank box again
        showLog.setText(prefs.getString("previous", ""));
    }
    // Store the latest trip that was added
    editor.putString("previous-trip", someId);
    // Store everything that's in the box so far for next time
    editor.putString("previous", showLog.getText().toString());
    // Commit to the prefs
    editor.commit();