java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList

Issue detail:
I am parsing the List using gson in kotlin with below syntax:
val methods: ArrayList<DataPOJO> = Gson().fromJson(dataObj.getString(withdraw_methods),
Array<DataPOJO>::class.java).toList() as ArrayList<DataPOJO>
Here, It's working fine while API response "dataObj.getString(withdraw_methods)" string array contains more than one object.
In case of one object its giving below error: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList

Solution:
val methods: ArrayList<DataPOJO> = ArrayList(Gson().fromJson(dataObj.getString(withdraw_methods), Array<DataPOJO>::class.java).toList())

Best article to understand SingletonList Vs. List::of -- Collections :: Singleton List Showdown

Detailed error log Android Studio:
02-20 13:35:20.370 17010-17010/com.myandriodapp W/System.err: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList
02-20 13:35:20.372 1483-1507/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{c8e1655 u0 com.myandriodapp/.v3_kotlin.cashback.user_activity.ProfileActivity t262} time:51048610
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:199)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.myandriodapp.v3_kotlin.cashback.user_activity.viewmodel.WithdrawalViewModel$AppWithdrawMethodsAPI$1.invokeSuspend(WithdrawalViewModel.kt:101)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.os.Handler.handleCallback(Handler.java:742)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.os.Looper.loop(Looper.java:157)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5603)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

custom spinner adapter android kotlin

Spinner Custom Item Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageViewFlag"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        app:layout_constraintEnd_toStartOf="@+id/textViewCountryName"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/textViewCountryName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Demo"
        android:textColor="#000"
        android:padding="7dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/imageViewFlag"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Spinner Custom Adapter
package com.example.customspinnerexamplekotlin

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.item_custom_spinner.view.*


class CustomSpinnerAdapter(ctx: Context, countries: ArrayList<CountryData>) : ArrayAdapter<CountryData>(ctx, 0, countries) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        return createItemView(position, convertView, parent);
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        return createItemView(position, convertView, parent);
    }

    fun createItemView(position: Int, recycledView: View?, parent: ViewGroup):View {
        val country = getItem(position)

        val view = recycledView ?: LayoutInflater.from(context).inflate(
                R.layout.item_custom_spinner,
                parent,
                false
        )

        country?.let {
            view.imageViewFlag.setImageResource(country.flag)
            view.textViewCountryName.text = country.countryName
        }
        return view
    }
}

CountryData Object
package com.example.customspinnerexamplekotlin

class CountryData (val countryName: String,
                   val flag: Int)

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f3f3f3">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tv_selected_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Select Country from below spinner"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@id/guidelineStart"
        app:layout_constraintTop_toTopOf="parent">
    </com.google.android.material.textview.MaterialTextView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#ffffff"
        android:padding="5dp"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@+id/guidelineStart"
        app:layout_constraintTop_toBottomOf="@id/tv_selected_item">

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt
package com.example.customspinnerexamplekotlin

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        setCustomAdapterSpinner()
    }

    fun setCustomAdapterSpinner() {

        val country_list = arrayListOf<CountryData>()

        country_list.add(CountryData("India", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("United States", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("Indonesia", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("France", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("China", R.drawable.ic_flag_black_24dp))

        val adapter = CustomSpinnerAdapter(
                this,
                country_list
        )

        spinner.adapter = adapter

        spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, p1: View?, pos: Int, p3: Long) {
                Toast.makeText(this@MainActivity, "" + (parent?.getItemAtPosition(pos) as CountryData).countryName, Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
            }
        })

        // dynamically adding data after setting adapter to spinner

        country_list.add(CountryData("Japan", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("New Zealand", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("Other", R.drawable.ic_flag_black_24dp))

        adapter.notifyDataSetChanged()
    }

}

android kotlin custom spinner adapter
Android kotlin Custom Spinner Adapter example


custom spinner with image and text android kotlin
Custom spinner Adapter with image and text android kotlin



android spinner example kotlin

android spinner example - string array xml

res/values/strings.xml
<resources>
    <string name="app_name">KotlinSpinnerExample</string>

    <string-array name="country_arrays">
        <item>India</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>China</item>
        <item>Japan</item>
        <item>New Zealand</item>
        <item>Other</item>
    </string-array>

</resources>

Populate the Spinner with string array choices

With an array such as mentioned above, you can use below code in your Activity or Fragment to set spinner with the string array using an ArrayAdapter instance:

package com.example.kotlinspinnerexample

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {

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

        setSimpleSpinner()
    }

    fun setSimpleSpinner() {
        // Create an ArrayAdapter using the string array (country_arrays) and a default spinner layout
        ArrayAdapter.createFromResource(
                this,
                R.array.country_arrays,
                android.R.layout.simple_spinner_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            // Apply the adapter to the spinner
            spinner.adapter = adapter
        }

       spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, p1: View?, pos: Int, p3: Long) {
                Toast.makeText(this@MainActivity, "" + parent?.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show()
            }
            override fun onNothingSelected(p0: AdapterView<*>?) {
            }
        })
    }
}


Here, I have put the spinner inside the ConstraintLayout and guildeline is on both sides. ConstraintLayout  has white background  and wraps AppCompatSpinner. So I can highlight the spinner according to background (background is grey here).
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#f3f3f3">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tv_selected_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Select Country from below spinner"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@id/guidelineStart"
        app:layout_constraintTop_toTopOf="parent">
    </com.google.android.material.textview.MaterialTextView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#ffffff"
        android:padding="5dp"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@+id/guidelineStart"
        app:layout_constraintTop_toBottomOf="@id/tv_selected_item">

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin spinner adapter
android kotlin - Spinner example

set string array to spinner android xml
Populate the Spinner with country string array

how to set value in spinner dynamically in android

var country_list = arrayListOf<String>()

        country_list.add("India")
        country_list.add("United States")
        country_list.add("Indonesia")
        country_list.add("France")
        country_list.add("China")
        country_list.add("Japan")
        country_list.add("New Zealand")
        country_list.add("Other")

val adapter = ArrayAdapter(
                this, // Context
                android.R.layout.simple_spinner_dropdown_item, // Layout
                country_list // ArrayList
        )

spinner.adapter = adapter
        

How to use Kotlin Coroutines with volley

How to use API connection with Kotlin Coroutines + Volley
When start developing API with Volley in Kotlin, you want to use Kotlin Coroutine instead of AsyncTask. We see how to use API connection using Volley and Coroutine as an example.
First of all, How can I take advantage of coroutines so I can write my code in concise manner:
Here's an example to Coroutine calling API Android

Volley overview

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley is an HTTP client developed by google for android development. You can use suspendCancellableCoroutine to make the Volley API request.

NetworkUtility.kt
package com.example.myapplication

import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import kotlinx.coroutines.suspendCancellableCoroutine
import org.json.JSONObject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

class NetworkUtility {
 
    companion object {
        val mRequestQueue: RequestQueue by lazy {
            Volley.newRequestQueue(MainApp.get())
        }

        suspend fun APIrequest(url: String): JSONObject {

            return suspendCancellableCoroutine { continuation ->
                try {
                    // Sucess Listner
                    val success = Response.Listener<JSONObject> { response ->
                        if (continuation.isActive) {
                            continuation.resume(response)
                        }
                    }

                    // Error Listner
                    val error = Response.ErrorListener { error ->
                        if (continuation.isActive) {
                            continuation.resume(JSONObject())
                        }
                    }

                    val jsonObjectRequest =
                        JsonObjectRequest(Request.Method.GET, url, null, success, error)

                    mRequestQueue.add(jsonObjectRequest)

                } catch (e: Exception) {
                    e.printStackTrace()
                    if (continuation.isActive) {
                        if (continuation.isActive) {
                            continuation.resumeWithException(e)
                        }
                    }
                }
            }
        }
    }
 
}

MainViewModel.kt
package com.example.myapplication

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class MainViewModel : ViewModel() {

    var usersList: MutableLiveData<ArrayList<User>> = MutableLiveData(arrayListOf())

    var isLoadingData = true
    var showProgress = MutableLiveData<Boolean>()


    fun getData(urlStr: String) {
        showProgress.value = true

        viewModelScope.launch(Dispatchers.IO) {
            val rss = NetworkUtility.APIrequest(urlStr)

            withContext(Dispatchers.Main) {
                // call to UI thread
                isLoadingData = false
                showProgress.value = false
                usersList.value?.addAll(parseJsonString(rss.toString()))
            }
        }
    }

    fun parseJsonString(str: String): ArrayList<User> {
        val jsonOb = JSONObject(str)
        val list: ArrayList<User> = arrayListOf()
        val jsonArray = jsonOb.getJSONArray("data");

        for (j in 0..jsonArray.length() - 1) {
            val user = User(
                jsonArray.getJSONObject(j).getString("id"),
                jsonArray.getJSONObject(j).getString("email"),
                jsonArray.getJSONObject(j).getString("first_name"),
                jsonArray.getJSONObject(j).getString("last_name"),
                jsonArray.getJSONObject(j).getString("avatar")
            )
            list.add(user)
        }
        return list
    }

}


MainActivity.kt
package com.example.myapplication

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar.*

class MainActivity : AppCompatActivity() {
    lateinit var viewModel: MainViewModel
    lateinit var userAdapter: UserRVAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)

        setSupportActionBar(toolbar)
        getSupportActionBar()?.setDisplayHomeAsUpEnabled(true)

        viewModel = ViewModelProviders.of(this)[MainViewModel::class.java]
        binding.mainVM = viewModel
        binding.setLifecycleOwner(this)

        viewModel.getData("https://reqres.in/api/users?page=1&per_page=12")

        viewModel.showProgress.observe(this, Observer {
            if (it == true) {
                progressBar.visibility = View.VISIBLE
            } else {
                progressBar.visibility = View.GONE
            }
        })

        viewModel.usersList.observe(this, Observer {
            Log.e("ResSize", "" + it.size + "::" + it)
            userAdapter.addList(it)
        })

        // Creates a vertical Layout Manager
        rv_users.layoutManager = LinearLayoutManager(this)
        userAdapter = UserRVAdapter(this)
        rv_users.adapter = userAdapter
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable name="mainVM" type="com.example.myapplication.MainViewModel" />

    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar" />

    <TextView
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        android:id="@+id/tv_select_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Users"
        android:layout_margin="7dp"
        android:textColor="@android:color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textSize="20sp"/>


    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/tv_select_user"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/rv_users"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <ProgressBar
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/progressBar"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

UserRVAdapter.kt
package com.example.myapplication

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.ly_item_user.view.*

class UserRVAdapter(val context: Context): RecyclerView.Adapter<UserRVAdapter.ViewHolder>() {

    var items : ArrayList<User> = arrayListOf()

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val posObj=items.get(position);

        holder.tv_name1?.text =posObj.first_name +" "+posObj.last_name
        holder.tv_email1?.text = posObj.email

        GlideApp.with(holder.iv_profile1.context)
            .load(posObj.avatar)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .apply(RequestOptions.circleCropTransform())
            .error(R.drawable.ic_launcher_background)
            .placeholder(R.drawable.ic_launcher_background)
            .into(holder.iv_profile1)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.ly_item_user, parent, false))
    }

    fun addList(itemsNew : ArrayList<User>){
            this.items=itemsNew
            notifyDataSetChanged()
            Log.e("ResSize:ItemsIF", "" + items.size)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
        // Holds the TextView that will add each animal to
        val tv_name1 = view.tv_name
        val tv_email1 = view.tv_email
        val iv_profile1 = view.iv_profile
    }
}

ly_item_user.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_profile"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="50dp"
        android:layout_margin="10dp"
        android:layout_height="50dp"/>

    <TextView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@id/iv_profile"
        app:layout_constraintBottom_toTopOf="@+id/tv_email"
        android:id="@+id/tv_name"
        android:textSize="20sp"
        android:text="Pradip"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/black"
        app:layout_constraintVertical_chainStyle="packed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_marginLeft="10dp"
        app:layout_constraintLeft_toRightOf="@id/iv_profile"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_name"
        android:id="@+id/tv_email"
        android:text="Tilala"
        android:textColor="@android:color/darker_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <View
        app:layout_constraintTop_toBottomOf="@id/iv_profile"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:background="#f3f3f3"
        android:layout_margin="10dp"
        android:layout_height="2dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Best reference : API connection with Volley + Coroutine

Popular Posts