Creating a custom list adapter

To show the output of the REST API, we need to create a custom list adapter and so we need to design an XML file of the custom list adapter. Here's the XML code for each row in the list:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp"
android:textAppearance="?android:textAppearanceMedium"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/contactNumber"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@tools:sample/full_names" />

<TextView
android:id="@+id/contactNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp"
android:textAppearance="?android:textAppearanceSmall"
app:layout_constraintBottom_toTopOf="@+id/email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name"
tools:text="@tools:sample/cities" />

<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp"
android:textAppearance="?android:textAppearanceSmall"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/contactNumber"
tools:text="@tools:sample/cities" />


</android.support.constraint.ConstraintLayout>

Here, we have a TextView of name, contactNumber, and email.

After that, we'll create the adapter, named UserListAdapter.kt, as follows:

class UserListAdapter(context: Context,
private val userList: List<UserModel>?) : BaseAdapter() {
private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val rowView = inflater.inflate(R.layout.user_list_item, parent, false)
val name = rowView.findViewById(R.id.name) as TextView
val email = rowView.findViewById(R.id.email) as TextView
val contactNumber = rowView.findViewById(R.id.contactNumber) as TextView
val userDetails = getItem(position) as UserModel
name.text = userDetails.name
email.text = userDetails.email
contactNumber.text = userDetails.contactNumber
return rowView
}
override fun getItem(position: Int): Any {
return userList!![position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return userList!!.size
}
}

This class extends BaseAdapter()which will add several inherited functions. 

Then you need to add LayoutInflaterwhich converts the XML layout into corresponding ViewGroups and Widgets: 

Now, in getView(), you'll add the element of the layout, as follows:

 val name = rowView.findViewById(R.id.name) as TextView
val email = rowView.findViewById(R.id.email) as TextView
val contactNumber = rowView.findViewById(R.id.contactNumber) as TextView

You should never perform long-running tasks on the main thread. This will result in an Application Not Responding (ANR).