简体   繁体   中英

Why won't `parcelize` pass data between my custom recycler adapter class and my custom activity class?

I am trying to pass a custom class instance from my custom recycler adapter class to a custom activity class using parcelize but not all of the custom class properties are being passed correctly. The instantiated class properties not being passed are properties which are instantiations of another custom class defined outside the main custom class. When I try to read values from the second custom class I get null

Here are my custom classes:

package com.riverstonetech.gositeuk.Model

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Site(var name: String? = null): Parcelable {

    var address: Address = Address("")
    var description: String? = null
    var locationCoordinate: Coordinate = Coordinate(0.0)
    var distance: Int? = null
    var price: Double? = null

}
package com.riverstonetech.gositeuk.Model

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Address(var line1: String? = null): Parcelable {

    var line2: String? = null
    var line3: String? = null
    var line4: String? = null
    var postcode: String? = null
    var phoneNumber: String? = null
    var siteURL: String? = null
}
package com.riverstonetech.gositeuk.Model

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Coordinate(var Latitude: Double? = null): Parcelable {
    var Longitude: Double? = null
}

Here is my custom recycler adapter class:

package com.riverstonetech.gositeuk

import android.content.Intent
import android.content.res.Resources
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import java.text.DecimalFormat

class SiteAdapter(
    private val mainActivity: CountiesActivity,
    private val siteList: List<Site>) : RecyclerView.Adapter<SiteAdapter.ListItemHolder>() {

    inner class ListItemHolder(val view: View):
        RecyclerView.ViewHolder(view),
        View.OnClickListener {

        internal var name = view.findViewById<View>(R.id.textViewSiteName) as TextView

        internal var distance = view.findViewById<View>(R.id.textViewSiteDistance) as TextView

        internal var price = view.findViewById<View>(R.id.textViewSitePrice) as TextView

        init {

            view.isClickable = true
            view.setOnClickListener(this)

        }

        override fun onClick(view: View) {

//            mainActivity.showDetails(adapterPosition)
            val intent = Intent(mainActivity, Details::class.java)
            intent.putExtra("SELECTED_SITE", siteList[adapterPosition])
            startActivity(mainActivity, intent, null)
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListItemHolder {

        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.recycler_list_item, parent, false)

        return ListItemHolder(itemView)

    }

    override fun getItemCount(): Int {

        if (siteList != null) {
            return siteList.size
        }
        // error
        return -1

    }

    override fun onBindViewHolder(holder: ListItemHolder, position: Int) {

        val site = siteList[position]

        holder.name.text = site.name
        holder.distance.text = site.distance.toString() + holder.view.context.getString(R.string.miles)
        val decimalFormatter = DecimalFormat("#,##0")
        holder.price.text = holder.view.context.getString(R.string.poundSign) + decimalFormatter.format(site.price).toString()

    }


}

and here is my custom activity class:

package com.riverstonetech.gositeuk

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_details.*

class Details : AppCompatActivity() {

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

        val site = intent.getParcelableExtra<Site>("SELECTED_SITE")
        Log.i("Info", "${site.name}")

        siteNameTextView.text = site.name

        Log.i("line 1", "${site.address.line1}")


    }
}

Every class used in your Site class needs to have the @Parcelize annotation too, and implement Parcelable . So you need to add that to Address and Coordinate too.

将我所有的属性声明移动到我的 3 个自定义类的主要构造函数中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM