如何在单独的活动中接收Parcelable对象参数?

时间:2018-10-06 12:32:48

标签: android kotlin parcelable

我希望能够在下一个活动中接收我的可分段对象的各个参数。我相信我使用的是正确的方法,只是不了解如何在第二个活动中一次输出参数输入。

我的MainActivty.kt:

package com.example.favouritefood

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageButton

class MainActivity : AppCompatActivity() {

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

        val food1: ImageButton = findViewById(R.id.imageButtonFood1)
        val food2: ImageButton = findViewById(R.id.imageButtonFood2)
        val food3: ImageButton = findViewById(R.id.imageButtonFood3)
        val food4: ImageButton = findViewById(R.id.imageButtonFood4)

        val pizza = Food(
                "Pizza", "www.test.com", "Italy, Tomato",
                "1/1/2018", "pizza@mail.com", 5)

        food1.setOnClickListener()
        {
            val intent = Intent(this, MetaDataActivity::class.java).apply {
                putExtra("Food", pizza)
            }
            startActivity(intent)
        }
    }
}

我的第二项活动(接收包裹):

package com.example.favouritefood

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.widget.EditText

class MetaDataActivity : AppCompatActivity() {

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

        val name: EditText = findViewById(R.id.editTextName)

        val food = intent.getParcelableExtra("Food") as Parcelable

        val test = food.writeToParcel(food, 0)

        name.setText(test.toString())
    }
}

food.wrtieToParcel(food, 0)的类型不匹配为Required:包裹!找到:可在food上包裹。

我将如何读取对象中的参数?

我的对象类供参考:

package com.example.favouritefood

import android.os.Parcel
import android.os.Parcelable

class Food(val name: String?, val location: String?, val keywords: String?, val date: String?, val email: String?, val rating: Int) : Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString(),
            parcel.readString(),
            parcel.readString(),
            parcel.readString(),
            parcel.readInt()) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeString(location)
        parcel.writeString(keywords)
        parcel.writeString(date)
        parcel.writeString(email)
        parcel.writeInt(rating)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Food> {
        override fun createFromParcel(parcel: Parcel): Food {
            return Food(parcel)
        }

        override fun newArray(size: Int): Array<Food?> {
            return arrayOfNulls(size)
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您不需要致电val test = food.writeToParcel(food, 0)

改为使用val food = intent.getParcelableExtra("Food") as Food