We will show a list of the user's details and the names of all the repos. Here, we will use ListView.
Here is the code of the acitivity_main.xml file:
<?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"
tools:context=".MainActivity">
<ListView
android:id="@+id/displayList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
We will use this listview in the onResponse() function of MainActivity.
We will get the list and create a custom adapter to show the user list, as shown in the following code:
val listItems = arrayOfNulls<String>( response.body()!!.size)
for (i in 0 until response.body()!!.size) {
val recipe = response.body()!![i]
listItems[i] = recipe.name
}
val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_list_item_1, listItems)
displayList.adapter = adapter
Here, we get the list of repos and convert them into an array. Then, we create the native adapter for the list with val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_list_item_1, listItems) and set the adapter in our list with displayList.adapter = adapter.
You should never perform long-running tasks on the main thread. It will incur an Application Not Responding (ANR) message.