PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How to Shuffle List Of Qoutes In Qoutes.json In Kotlin

 July 23, 2022     arrays, json, kotlin, shuffle     No comments   

Issue

hi i am new to kotlin and want to get shuffeled my qoutes found in asset here is my peice of code

private fun loadQuotesList(): Array<Quotes> {
        val inputStream = context.assets.open("quotes.json")
        val size :Int = inputStream.available() // find size of input stream
        val buffer = ByteArray(size)// define byte array and put size
        inputStream.read(buffer) // read byte array
        inputStream.close()

        val json =   String(buffer, Charsets.UTF_8) //convert bytearray to string by passing array & types

        val gson = Gson()

        return gson.fromJson(json,Array<Quotes>::class.java)
    }

Solution

Like this:

private fun loadQuotesList(): Array<Quotes> {
...
    val result = gson.fromJson(json,Array<Quotes>::class.java)
    result.shuffle()
    return result
}

or even better Kotlin syntax

private fun loadQuotesList(): Array<Quotes> {
...
   return gson.fromJson(json,Array<Quotes>::class.java).apply { shuffle() }
}


Answered By - AndrewL
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing