Question Best way to save EditText in Kotlin

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Hi there,

So I have another problem.

I made a button called "Save" which I want to save the input from an EditText. Then when the user reopens the application, I want it to set the text to what the user input before.

I thought of writing to a json file like in python, but I am not sure how to that in kotlin.

What is the best way to do this?

Thanks,
salm2s
 
Last edited:

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Homework?

More details needed.

There seems to be quite a bit of information available via "google".

E.g.:

https://bezkoder.com/kotlin-parse-json-gson/

https://hceris.com/painless-json-with-kotlin-and-jackson/

Write some code, test, and if there is a specific problem then post your code and ask for assistance.

Thanks for the info.

So i managed to save the text to a json file by doing this:

Code:
var Settings = JSONObject()

Settings.put("URL", Pooltxt.text)
Settings.put("User", Usertxt.text)
Settings.put("Passwd", Passwdtxt.text)
Settings.put("CPU", thrdstxt.text)

var file = File("/data/data/com.example.myapplication/files/config.json")

if(!file.exists()){
    file.createNewFile()
}
else if(file.exists()){
    file.writeText(Settings.toString())
}

This saves the data to JSONObject.

But I am confused as to how to read the data from the .json file.

I tried to do the following:

Code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    saveConfig()
    setText()
  }
....

fun setText() {
    var file = File("/data/data/com.example.myapplication/files/config.json") as JSONObject
    editText.setText(file.get("URL").toString())

}

But this just makes the app crash every time I open it.

Any way i can fix this?

Thanks,
salm2s
 

Ralston18

Titan
Moderator
Process now at:

Get data from EditText = Done
Save to .json file= Done
Use Kotlin to parse data from .json file = Work in Progress


Any error messages with respect to the crashes?

Are you able to put in some interim code to print variable values or otherwise find where/when the crash occurs?

You parsed the data into the .json file as I understand your code.

However I am not sure about your code to pull the parse and read the data.

You only want the URL to be saved and returned to the user - correct?

As before I looked for some example code to help with both logic and structure.

E.g.:

https://www.myandroidsolutions.com/2019/07/25/android-parse-json-file-from-assets/#.Xg0ApEBFyUk

Use Notepad to look at the .json file and locate the needed "pieces" to return to the User.

Even if you want to return all of it, just get one to work and that may serve as a model for the other pieces.
 

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Alright so I think i found a solution.

So instead of using inputStream and context.getAssests(), I instead used FileInputStream:

Code:
try {

            var context = applicationContext.filesDir.path
            val path = context + "config.json"

            val inputStream: InputStream =
                FileInputStream("$path")
            json = inputStream.bufferedReader().use { it.readText()}


            var jsobobj = JSONObject(json)

            for (i in 0..jsobobj.length() - 1) {
                var server = jsobobj.get("URL")
                var username = jsobobj.get("User")
                var password = jsobobj.get("Passwd")
                var threads = jsobobj.get("CPU")

                serverset.setText(server.toString())
                userrset.setText(username.toString())
                passwdset.setText(password.toString())
                thrdsset.setText(threads.toString())
}
} catch (e: IOException) {
e.printStackTrace()
}

Thanks for your help