Home > Enterprise >  How to GET data inside list<data> with another data as the condition to RecyclerView
How to GET data inside list<data> with another data as the condition to RecyclerView

Time:10-25

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

    }

CodePudding user response:

Fragment into set list in adapter Code below :

 binding.gvFolder.layoutManager = layoutManager
        binding.gvFolder.adapter = AdapterGalleryPhotosFolder(this@GalleryImageFolderActivity,al_images,activityAddress)

below my Model class code:

data class ModelGalleryImagesFolder(val name:String, var al_imagepath: ArrayList<String>)

and below my full adapter code:

class AdapterGalleryPhotosFolder(
    var context: Context,
    var arrayList: ArrayList<ModelGalleryImagesFolder>,
    var activityAddress: String?
):RecyclerView.Adapter<RecyclerView.ViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHodler(AdapterPhotosfolderBinding.inflate(LayoutInflater.from(context),parent,false))

    }
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        (holder as MyViewHodler)

        val modelImages = arrayList[position]

        holder.binding.tvFolder.text = modelImages.name
        holder.binding.tvFolder2.text = arrayList[position].al_imagepath.size.toString() " photo"

        Glide.with(context).load(arrayList[position].al_imagepath[0])
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .into(holder.binding.ivImage)


        holder.binding.secondlayout.setOnClickListener {
            val intent = Intent(context, GalleryImageFolderIntoImagesActivity::class.java)
            intent.putExtra("position", position)
            intent.putExtra("value", activityAddress)
                context.startActivity(intent)
            (context as Activity).finish()
        }
    }

    override fun getItemCount(): Int {
        return arrayList.size
    }
    class  MyViewHodler(var binding: AdapterPhotosfolderBinding) : RecyclerView.ViewHolder(binding.root)
}

CodePudding user response:

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

  • Related