Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] mutableListOf to Bundle (Kotlin)

Posted on November 14, 2022

Solution 1 :

You can implement Parcelable interface:

data class Book(val title: String, val id: Int) : Parcelable {
    constructor(source: Parcel) : this(
            source.readString()!!,
            source.readInt()
    )

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeString(title)
        writeInt(id)
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<Book> = object : Parcelable.Creator<Book> {
            override fun createFromParcel(source: Parcel): Book = Book(source)
            override fun newArray(size: Int): Array<Book?> = arrayOfNulls(size)
        }
    }
}

And use it like the following:

var books = mutableListOf<Book>()
val booksforDel = Bundle()
booksforDel.putParcelableArray("books", books.toTypedArray())

Ann to retrieve books in a Fragment:

val booksForDelete = arguments?.getParcelableArray("books")

Problem :

I have a mutableLIst:

var books = mutableListOf<Book>()

model “Book” is:

data class Book(val title: String, val id: Int)

My code is:

      button2.setOnClickListener{
        val delFragment = DelFragment()
        val booksforDel = Bundle()
        booksforDel.putStringArrayList("books", books as ArrayList<String>)
        delFragment.setArguments(booksforDel)

        val manager = supportFragmentManager
        delFragment.show(manager,"Delete Book")
    }

in Fragment I try to get data:

val booksForDelete = getArguments()?.getStringArrayList("books")!!

And get Error:

java.lang.ArrayStoreException: source[0] of type com.example.http_example.model.Book cannot be stored in destination array of type java.lang.String[]

How send a data from mutableList “books” to Bundle in DialogFragment?

Comments

Comment posted by Дмитрий

it’s work. how to organize setSingleChoiceItems for bookForDelete? I make a code: .setSingleChoiceItems(booksForDelete?.map(Book::title)?.toTypedArray(),checkedItem){ dialog, which -> Toast.makeText(activity,”Choosen book: ${booksForDelete?.map { Book::title}?.get(which)}”,Toast.LENGTH_SHORT).show() } IDE marked “map” in the first line

Comment posted by Sergio

Please create another question on Stackoverflow and include all information there, give me a link here, and I’ll try to help.

Comment posted by Дмитрий

ok. i try. я вам очень признателен за помощь. потому как только осваиваю Kotlin.

Comment posted by momvart

instead of implementing

READ  [ANSWERED] java - Can Android databinding getters return specific fields dynamically based on the getter's argument?
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress