0

I tried to take a string from list but I want to separated that string become 2. This is my list [Aquaman (1), Bumblebee (3), Spiderman into the spider verse (4), Bohemian Rhapsody (7), A Star Is Born (8), Mary Poppins Returns (9), Captain Marvel (10), Ralph Breaks the Internet (11), Avengers: Endgame (12)]

I want to separate the title and the number on it become 2 string but there are still one because I want to use the title for view and I want to use the number for id that I want to post. Can I do that? Please help.

This is my model

 class Movie2 (
        @SerializedName("id")
        var movieId: String? = null,
        @SerializedName("description")
        var synopsis: String? = null,
        @SerializedName("release_date")
        var release: String? = null,
        @SerializedName("poster")
        var poster: String? = null,
        @SerializedName("genre")
        var genre: String? = null,
        @SerializedName("title")
        var title: String? = null
) 
{
        override fun toString(): String {
                return "$title ($movieId)"
        }
}

this is where i have to put the data

override fun showMovieList(data: List<Movie2>) {
    movies = data[0]
    reviews.clear()
    reviews.addAll(data)
    data.forEach {
         title = it.title!!
        id = it.movieId!!
    }
    Log.v("id", "" + id)
    Log.v("title", "" + title)
    searchSpn.adapter = ArrayAdapter<Movie2>(this, R.layout.spinner_item, reviews)
    movie = searchSpn.selectedItem.toString()

}
Qube
  • 543
  • 3
  • 9
  • 23

2 Answers2

0
val lData = listOf<String>()
lData.forEach {
  val data = it.split("(")
  val id = data[1].split(")")[0]
  val title = data[0]
}

its better you create class model like "Movie" with proprety uid and title or you can keep in hashMap() like this

val lData = listOf<String>()
val lisofMovie = mutableListOf<HashMap<String, String>>()

lData.forEach {
  val data = it.split("(")

  val id = data[1].split(")")[0]
  val title = data[0]

  val lMovie = HashMap<String, String>()
  lMovie["id"] = id
  lMovie["title"] = title

  lisofMovie.add(lMovie)
}
Rofie Sagara
  • 289
  • 5
  • 17
  • actually i already make a model for it but it only can be print when i make it `toString` so i seprated it with `()` but don't have an idea to really seprated it into 2 string but still one – Qube Jan 27 '19 at 12:22
  • why you need toString() for print. why not just bring the model to list? – Rofie Sagara Jan 27 '19 at 12:25
  • val data = it.split("(") val id = data[1].split(")")[0] val title = data[0] – Rofie Sagara Jan 27 '19 at 12:27
  • because it don't want to print the right string like in my case here https://stackoverflow.com/questions/54358667/how-to-parse-data-from-json?noredirect=1#comment95532314_54358667 – Qube Jan 27 '19 at 12:27
  • you mean to show in listview or something so you use toString() – Rofie Sagara Jan 27 '19 at 12:30
  • i think that i can use data.foreach to call it. but i have to make the data that can be put into ArrayAdapter – Qube Jan 27 '19 at 12:43
  • You can use movie = searchSpn.selectedItem as Movie2 movie is object from Movie2. Because you put Movie2 in adapter just delete the toString() – Rofie Sagara Jan 27 '19 at 16:14
0

Its not quite clear what your issue is but you seem to want to use just the title in the spinner, then one way to do this would be to override toString as you have done, but it sounds like you just want the title, so I think it should be.

   override fun toString(): String {
            return title?:"no title provided"
    }

This does not change your underling list, or the objects in the list, so when you select an item you can get it via onItemSelected method, for example from the documentation.

class SpinnerActivity : Activity(), AdapterView.OnItemSelectedListener {

    override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    override fun onNothingSelected(parent: AdapterView<*>) {
        // Another interface callback
   }
}

In your case you should get an instance of your Movie2 class, and then you can get the id my just calling parent.movieId

nPn
  • 16,254
  • 9
  • 35
  • 58