Home > Mobile >  Intent all data when ListView is clicked
Intent all data when ListView is clicked

Time:12-22

I have this Android app using Kotlin, that populates the ListView to display the movie titles. The problem I am currently facing is that I am not sure on how to Intent all the data related to the ListView title. An example would be like this, if I click on the "Jumanji" title, the app start the new activity and display all the information related to that movie title clicked

enter image description here

enter image description here

So far I am only able to populate the ListView and Intent only the title of the movie clicked, but not sure how to do the rest

Activity where I populate the list of movies:

class SimpleViewListOfMoviesActivity : AppCompatActivity() {

    val INTENT_CODE = 1;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_list_of_movies)

        val movies = simpleMovieitemArray
        val movie_tiles = movies.map {it.title}
        // val movie_overviews = movies.map {it.overview} (testing ignore)

        val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, movie_tiles)
        movielist.adapter = listAdapter

        movielist.onItemClickListener = object : AdapterView.OnItemClickListener {
            override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
            {
//              displayToast("You have selected "   parent?.adapter?.getItem(position))
                MovieIntent(parent?.adapter?.getItem(position) as String)
            }

        }
    }


    fun displayToast(message:String){

        Toast.makeText(this,message, Toast.LENGTH_LONG).show()
    }

    fun MovieIntent(message:String)
    {
        var myIntent = Intent(this, SimpleItemDetailActivity::class.java)
        myIntent.putExtra("movieTitle", message)
        startActivityForResult(myIntent,INTENT_CODE)
    }

}

Activity where I get the data through Intent and display

class SimpleItemDetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.simple_activity_item_detail)

        var movieTitleFromList = intent.getStringExtra("movieTitle")
        movie_title.text = "$movieTitleFromList"
    }

Where the data comes from"

class SimpleMovieSampleData {



    companion object{

        var simpleMovieitemArray : ArrayList<SimpleMovieItem>
        init {

            simpleMovieitemArray = ArrayList<SimpleMovieItem>()
            populateSimpleMovieItem()
        }



        fun populateSimpleMovieItem() : ArrayList<SimpleMovieItem>{
simpleMovieitemArray.add(
                SimpleMovieItem("Elsa, Anna, Kristoff and Olaf head far into the forest to learn the truth about an ancient mystery of their kingdom.",
                    "November 22, 2019",
                    "English",
                    "Frozen II (2019)")
            )

            simpleMovieitemArray.add(
                SimpleMovieItem("In Jumanji: The Next Level, the gang is back but the game has changed. As they return to rescue one of their own, the players will have to brave parts unknown from arid deserts to snowy mountains, to escape the world's most dangerous game.",
                    "December 13, 2019",
                    "English",
                    "Jumanji: The Next Level")
            )
//two examples of the movies
            return simpleMovieitemArray
        }

    }
}

CodePudding user response:

I did this and it works, but it is not efficient and I have no clue how to shorten it:

override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
        {
            var overview = movies[0].overview.toString()
            var release_date = movies[0].release_date.toString()
            var language = movies[0].original_langauge.toString()
            var title = parent?.adapter?.getItem(position)


            if (title == movies[1].title)
            {
                overview = movies[1].overview.toString()
                release_date = movies[1].release_date.toString()
                language = movies[1].original_langauge.toString()
                //continue all the way till the end
            }

CodePudding user response:

You can make SimpleMovieItem object Parcelable, and when you click one item, you can create an Intent that put your SimpleMovieItem object to the bundle extra of your intent:

intent.putExtra("key", Bundle().apply {
            putParcelable("object", simpleMovieItemObject)
        })

And in your new Activity you can get this object and do everything you want.

  • Related