Mario Muresean
Mario Muresean

Reputation: 273

Cast a specific object to another

I'm trying to cast one object to another, that are the same(same attributes) but I keep getting that I can't do this cast. I have these two objects, and one adapter that uses PatientFile as the main object but I want to use i for DoctorFile too because is the same object, but when I try to cast it, it just keep showing the cast cannot be succeded.

So my question is how can I cast the object DoctorFile to PatientFile that are the same object and use it in my adapter as a list. And if you ask I can't create only one object because these 2 are received from a petition from retrofit.

Object 1:

 data class DoctorFiles(
   val file: String,
   val id: String,
   val name: String
): Serializable

Object 2:

 data class PatientFiles(
   val file: String,
   val id: String,
   val name: String
): Serializable

Adapter:

class ConsultationDocumentsAdapter(private val onDownload: (PatientFilesId)->Unit):RecyclerView.Adapter<ConsultationDocumentsHolder>() {
private var list: List<PatientFilesId> = listOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ConsultationDocumentsHolder {
    val inflater = LayoutInflater.from(parent.context)
    return ConsultationDocumentsHolder(inflater, parent, onDownload)
}

override fun onBindViewHolder(holder: ConsultationDocumentsHolder, position: Int) {
    holder.bind(list[position])
}

override fun getItemCount(): Int = list.size

fun loadItems(documents: List<PatientFilesId>) {
    list = documents
    notifyDataSetChanged()
}

}

class ConsultationDocumentsHolder(
  inflater: LayoutInflater,
 parent: ViewGroup,
 private val onDownload: (PatientFilesId) -> Unit
 ) :
   RecyclerView.ViewHolder(inflater.inflate(R.layout.cell_consultation_documents, 
parent, false)) {

private lateinit var documentName: TeladocTextView
private lateinit var documentSize: TeladocTextView
private lateinit var documentDownload: AppCompatImageView

fun bind(document: PatientFilesId) {
    setupUi()
    loadConsultationDocumentData(document)
}

private fun setupUi() {
    documentName = itemView.findViewById(R.id.consultation_document_name)
    documentSize = itemView.findViewById(R.id.consultation_document_size)
    documentDownload = itemView.findViewById(R.id.consultation_document_download)
}

private fun loadConsultationDocumentData(document: PatientFilesId) {
    documentName.text = document.name
    val size = "" + TeladocUtils.sizeOfFile(document.file) / 1024
    documentSize.text = size + "Mb"

    documentDownload.setOnClickListener{
        onDownload(document)
    }
}

}

Upvotes: 1

Views: 1775

Answers (6)

Hurman Iqbal
Hurman Iqbal

Reputation: 61

YOu can try Extension function for this. But if you want another way to do this then this is what i found.

data class DataX(
    val Rating: String?,
    val auther_name: String?,
    val author_id: Int?,
    val category: List<Category>?,
    val created_at: String?,
    val description: String?,
    val epublink: String?,
    val id: Int?,
    val image: String?,
    val is_publish: Int?,
    val lang: String?,
    val listing_text: String?,
    val pages: String?,
    val parent_id: Int?,
    val price: String?,
    val publisher_name: String?,
    val slug: Any?,
    val start_date: String?,
    val title: String?,
    val type: String?,
    val updated_at: String?,
    val url: String?
):Serializable

data class Book(
    val Rating: String,
    val auther_name: String,
    val author_id: Int,
    val category: List<Category>,
    val created_at: String,
    val description: String,
    val epublink: String,
    val id: Int,
    val image: String,
    val is_publish: Int,
    val lang: String,
    val listing_text: String,
    val pages: String,
    val parent_id: Int,
    val price: String,
    val publisher_name: String,
    val slug: Any,
    val start_date: String,
    val title: String,
    val type: String,
    val updated_at: String,
    val url: String
) : Serializable

These are my two data classes.

I Converted book of type Book to json string by using

val book= Gson().toJson(book)

and the converted bake to object of DataX class by using

book = Gson().fromJson(args.book, DataX::class.java)

Upvotes: 0

Henry Twist
Henry Twist

Reputation: 5980

You can't cast them I'm afraid. Even if they have the same properties, a DoctorFile isn't a PatientFile.

What would be sensible is to set up an inheritance structure, so both PatientFile and DoctorFile inherit from a File with the relevant properties, or simply copy the properties into a new object.

Upvotes: 4

AamirR
AamirR

Reputation: 12198

You cannot cast different types unless these classes inherit from a same super-class.

I would recommend an abstract non-data base-class:

abstract class CommonFile: Serializable {
    abstract val file: String
    abstract val id: String
    abstract val name: String
}

And inherit data classes from super-class.

data class DoctorFiles (
   override val file: String,
   override val id: String,
   override val name: String
): CommonFile()

data class PatientFiles (
   override val file: String,
   override val id: String,
   override val name: String
): CommonFile()

Usage:

val doctorsFile = DoctorFiles("1", "1", "Jane Doe")

// cast to super-class
val commonFile = doctorsFile as CommonFile

// Check which data class
when (commonFile) {
    is DoctorFiles -> print("This is a DoctorFiles")
    is PatientFiles -> print("This is a PatientFiles")
}

// Cast multiple data classes
val doctorsFile = DoctorFiles("1", "1", "Jane Doe")
val patientsFile = PatientsFile("2", "2", "Joe Blow")

val list = listOf<CommonFile>(doctorsFile, patientsFile)

Upvotes: 3

Nicholas Anyanwu
Nicholas Anyanwu

Reputation: 1

This is one method I know you can use to cast to objects in koltin

val patientFiles = PatientFiles()
val doctorFiles = DoctorFiles()

patientFiles.file = doctorFiled.file
doctoreFiles.name = patientFiles.name

Upvotes: 0

Laze Wu
Laze Wu

Reputation: 39

You can try

list.map {it->PatientFiles(it.file,it.id,it.name)}

Upvotes: 1

Arpit Shukla
Arpit Shukla

Reputation: 10493

If you want to convert (you can't cast here) DoctorFile to PatientFile, you can create a simple extension function for that.

fun DoctorFile.toPatientFile() = PatientFile(file, id, name)

Upvotes: 1

Related Questions