Hello Im working on project using Kotlin (for the first time). My problem is i don't know how to get data "fullname" inside "data" list where one of the data inside the list also getting use for the condition, the name is "Role" to my RecyclerView in my Activity.
So i need show list "fullname" inside my RecyclerView where the "Role" is "Patient".
Here is the DataFileRecord:
data class DataFileRecord(
@field:SerializedName("total")
val total: Int? = null,
@field:SerializedName("data")
val data: List<DataItem>? = null,
@field:SerializedName("offset")
val offset: Int? = null,
@field:SerializedName("limit")
val limit: Int? = null,
@field:SerializedName("status")
val status: Int? = null
)
//get fullname
data class DataItem(
@field:SerializedName("updateDate")
val updateDate: String? = null,
@field:SerializedName("departement")
val departement: Departement? = null,
@field:SerializedName("isBlock")
val isBlock: String? = null,
@field:SerializedName("role")
val role: Role? = null,
@field:SerializedName("fullname")
val fullname: String? = null,
@field:SerializedName("id")
val id: String? = null,
@field:SerializedName("username")
val username: String? = null
)
//get role = patient
data class Role(
@field:SerializedName("name")
val name: String? = null,
@field:SerializedName("id")
val id: String? = null
)
data class Departement(
@field:SerializedName("name")
val name: String? = null,
@field:SerializedName("id")
val id: String? = null
)
Here is my Adapter:
class PatientAdapter(val context: List<DataItem>) : RecyclerView.Adapter<PatientAdapter.MyViewHolder>() {
var patientList: List<DataItem> = listOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_patient,parent,false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return patientList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int){
holder.patientName.text = patientList.get(position).fullname
}
@SuppressLint("NotifyDataSetChanged")
fun setPatientListItems(patientList: List<DataItem>){
this.patientList=patientList
notifyDataSetChanged()
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val patientName: TextView = itemView.findViewById(R.id.itemPatientName)
}
}
And here is where i stuck with my Call api in my activity:
//this 4 lines are inside oncreate
patientRecyclerView.setHasFixedSize(true)
linearLayoutManager = LinearLayoutManager(this)
patientRecyclerView.layoutManager = linearLayoutManager
patientRecyclerView.adapter = recyclerAdapter
fun getPatient(apiKey: String, apiSecret: String, token: String){
val retrofit = RetrofitClient.getClient()
retrofit.addGetPatient(apiKey,apiSecret,token)
.enqueue(object : Callback<DataFileRecord>{
override fun onResponse(call: Call<DataFileRecord>, response: Response<DataFileRecord>) {
TODO("Not yet implemented")
val dataPatient = response.body()
if (response.isSuccessful){
---------------- HERE WHERE I STUCK --------------------
}
}
override fun onFailure(call: Call<DataFileRecord>, t: Throwable) {
TODO("Not yet implemented")
}
})
}
Got the answer.. just put this inside response= isSuccessfull
namePatient?.data?.filter { it.role?.name=="Pasien" }.let { if (it != null) { dataP.addAll(it) } }
reciAdapter.notifyDataSetChanged()
So "dataP" is my lateinit var MutableList, and "reciAdapter" is my Recycler Adapter Class