将特定对象投射到另一个对象

Cast a specific object to another

我正在尝试将一个对象转换为另一个对象,它们是相同的(相同的属性),但我一直了解到我无法执行此转换。 我有这两个对象和一个使用 PatientFile 作为主要对象的适配器,但我也想将 i 用于 DoctorFile 因为是同一个对象,但是当我尝试转换它时,它只是一直显示转换无法成功。

所以我的问题是如何将同一个对象的对象 DoctorFile 转换为 PatientFile 并在我的适配器中将其用作列表。如果你问我不能只创建一个对象,因为这 2 个是从改造的请愿书中收到的。

对象 1:

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

对象 2:

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

适配器:

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)
    }
}

}

恐怕你不能施放它们。即使它们具有相同的属性,DoctorFile 也不是 PatientFile.

明智的做法是设置一个继承结构,因此 PatientFileDoctorFile 都从具有相关属性的 File 继承,或者简单地将属性复制到一个新对象。

如果你想转换(你不能在这里转换)DoctorFilePatientFile,你可以为此创建一个简单的扩展函数。

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

你可以试试

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

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

除非这些 classes 继承自同一个超级 class.

,否则您不能强制转换不同的类型

我会推荐一个抽象的非数据库-class:

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

并从超级 class.

继承数据 classes
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()

用法:

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)