0

very simple, android kotlin. i have a file in the project assest folder with sentence in each line. what i wants, is when i open dialog, it will select random line and put it as the dialog message. i couldn't find any proper solution. dialog's code:

class JokeFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val sentence: String = //random line from the file
            // Use the Builder class for convenient dialog construction
            val builder = Builder(it)
            builder.setMessage(sentence)
                .setNegativeButton(R.string.cancel){ _, _->}
            // Create the AlertDialog object and return it
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

nope
  • 106
  • 9
  • Is there any reason you cannot make a String Array resource with your string lines? Then you can pull a random string from the array. If you put it in a text file, you will have to do file IO which would involve having to do load the file in a background thread first and then updating the dialog back on the main thread after the file is loaded. String resources are preloaded at app startup, so you don't have to worry about using background threads to retrieve them. – Tenfour04 Nov 29 '22 at 15:25
  • I see no file. I dont see you opening a file. I dont see you reading a file. You did not enough. – blackapps Nov 29 '22 at 15:31
  • sure, i know how read to entire file, but at line 5 I asked for help – nope Nov 29 '22 at 15:33
  • `which would involve having to do load the file in a background thread first`? @Tenfour04. Not needed at all. Maybe if the file has megabyte size. Further typing all data in a string array is terrible work. No a file is perfect and flexible. – blackapps Nov 29 '22 at 15:35
  • For line 5 stop at .readLine() for the fifth time. How many lines in the file? – blackapps Nov 29 '22 at 15:36
  • i meant the line 5 of the code above – nope Nov 29 '22 at 15:36
  • Yes i saw that. But i see no file. I see no... Well i already told you. – blackapps Nov 29 '22 at 15:37
  • the file in in the internal storage assets folder – nope Nov 29 '22 at 15:39
  • @blackapps, I’m assuming a megabyte or more size since it looks like a joke of the day sort of app so it should have hundreds of lines. Reading such a file would add a little bit of jank to the UI thread. Typing out a string array resource would be terrible, but of course you would write a script to create it for you. – Tenfour04 Nov 29 '22 at 15:53
  • If it is the assets folder then you should have told that of course on the first line of your post. – blackapps Nov 29 '22 at 15:54

1 Answers1

1

Based on this answer:

fun readRandomLineFromAsset(context: Context, fileName: String): String =
        context
            .assets
            .open(fileName)
            .bufferedReader()
            .use(BufferedReader::readText)
            .lines()
            .shuffled()
            .first()

Updated code - shuffled().first() instead of random()

Random picks same line every time fo some reason.

bylazy
  • 1,055
  • 1
  • 5
  • 16