skip to Main Content

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 perform this to other values available.

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
        }

    }
}

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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
                }
    
    Login or Signup to reply.
  3. save:

    Gson gson = new Gson();
    intent.putExtra("key",gson.toJson( movielist.get(position)));
    

    Read:

    SimpleMovieItem movies;
    String movieData=(getIntent().getStringExtra("key")));
    
         Type userType = new TypeToken<SimpleMovieItem>() {
                }.getType();
                movies= gson.fromJson(movieData, userType);
    
                    overview = movies.overview.toString()
                    release_date = movies.release_date.toString()
                    language = movies.original_langauge.toString()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search