3
\$\begingroup\$

The code is completely functional but I'd rather not call two functions when I know it can be done more efficiently. My goal is to store EditText values in SharedPrefs using a single addTextChangedListener. Any advice is much appreciated!

fun preserveText() {


    val sharedPref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

    field_1.setText(sharedPref.getString("autoSave", ""))
    field_1.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable) {
            sharedPref.edit().putString("autoSave", s.toString()).apply()


        }
    })
}

fun preserveText2() {


    val sharedPref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

    field_2.setText(sharedPref.getString("autoSave2", ""))
    field_2.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable) {
            sharedPref.edit().putString("autoSave2", s.toString()).apply()


        }
    })
}
\$\endgroup\$
5
  • \$\begingroup\$ Please clarify: This code works fully as intended? \$\endgroup\$
    – Vogel612
    Commented Aug 4, 2017 at 8:59
  • \$\begingroup\$ Yes it works perfectly. However I'd like to have the same functionality using one method instead of two. Ideally I'd like to have a generic method that I can pass a EditText and String variable as the input. I haven't been able to figure that part out. \$\endgroup\$
    – Dallas
    Commented Aug 4, 2017 at 15:19
  • \$\begingroup\$ Should this question really have the "java" tag? As far as I understand, it doesn't have anything to do with Java itself. Kotlin may be compiled to JVM byte code, but the language "Java" and the interpreter "JVM" are two different things and the code posted doesn't seem to be Java code. I would remove the tag myself, but I'm not sure if it really is misplaced. \$\endgroup\$
    – Stingy
    Commented Aug 5, 2017 at 14:31
  • \$\begingroup\$ I think anyone that knows Java or Kotlin can help me combine these two functions into one and/or another way altogether. \$\endgroup\$
    – Dallas
    Commented Aug 6, 2017 at 5:43
  • \$\begingroup\$ I would think that, in order to help with a problem in a programming language, one should also know that language. I know Java but not Kotlin, so I wouldn't even have read this question had I known that it's not about Java but about Kotlin. But if my ignorance of Kotlin doesn't bother you, I would suggest to pass field_1 or field_2 as a parameter to the method preserveText and create a link between those fields and the strings "autoSave" and "autoSave2" somewhere else, either through a map, or by making this string a field of field_1/field_2. \$\endgroup\$
    – Stingy
    Commented Aug 6, 2017 at 13:56

2 Answers 2

0
\$\begingroup\$

I'm not sure if what I write below is what you are looking for. Also I don't know much about Android, so maybe that would not work for some reason.

preserveText(field1, "autoSave")
preserveText(field2, "autoSave2")

val sharedPref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

fun preserveText(field: Type????, itemName: String) {
    field.setText(sharedPref.getString(itemName, ""))
    field.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { }

        override fun afterTextChanged(s: Editable) {
            sharedPref.edit().putString(itemName, s.toString()).apply()
        }
    })
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks Stingy and toto2! This is exactly what I was looking for. \$\endgroup\$
    – Dallas
    Commented Aug 9, 2017 at 18:54
0
\$\begingroup\$

My actual Kotlin code now:

    storeEditTextField(field_1, "EDIT_TEXT_KEY_1")
    storeEditTextField(field_2, "EDIT_TEXT_KEY_2")


fun storeEditTextField(editTextView: EditText, editTextID: String) {

    val sharedPref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

    editTextView.setText(sharedPref.getString(editTextID, ""))
    editTextView.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable) {
            sharedPref.edit().putString(editTextID, s.toString()).apply()


        }
    })
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.