Generics Array
Generics Array
accessible using different data types while keeping a check of the compile-time type safety. Creating
parameterized classes – A generic type is a class or method that is parameterized over types. We
always use angle brackets <> to specify the type parameter in the program. Generic class is defined as
follows:
class MyClass<T>(text: T) {
var name = text
}
If the parameters can be inferred from the arguments of constructor, one is allowed to omit the type
arguments:
val my = MyClass("HELLO")
Here, HELLO has type String, so the compiler figures out that we are talking about Myclass<String>
Advantages of generic –
Generic Arrays
Generic arrays in Kotlin are represented by Array<T>.
To create an array with given size and initial values, use the constructor:
strings.set(2, "ChangedItem")
print(strings.get(2)) // prints "ChangedItem"
The main purpose of the adapter is to fetch data from an array or database and insert each item that
placed into the list for the desired result. So, it is main source to pull data from strings.xml file which
contains all the required strings in Kotlin or xml files.
Android Adapter
Adapter holds the data fetched from an array and iterates through each item in data set and generates
the respective views for each item of the list. So, we can say it act as an intermediate between the data
sources and adapter views such as ListView, Gridview.
ArrayAdapter: It always accepts an Array or List as input. We can store the list items in the strings.xml
file also.
CursorAdapter: It always accepts an instance of cursor as an input means
SimpleAdapter: It mainly accepts a static data defined in the resources like array or database.
BaseAdapter: It is a generic implementation for all three adapter types and it can be used in the views
according to our requirements.
activity_main.xml file
In this file, we declare the LisitView within LinearLayout and set its attributes. Later, we will access
the ListView in Kotlin file using the id.
import android.widget.ArrayAdapter
import android.widget.ListView
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
arrayAdapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1, users)
mListView.adapter = arrayAdapter
The complete project structure with the file locations is as shown in the following screenshot.
Step 1
Create an Android project with Empty Activity. We are keeping the default values for Name and
Package name.
Add RecyclerView widget to activity_main.xml. Please note the id we provided for this RecyclerView
widget.
The width and height of this RecyclerView widget match the parent.
activity_main.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3
Create a layout file item.xml. This layout file is used to display each item in the RecylcerView
widget.
Each item in the RecyclerView is displayed as a LinearLayout with a single TextView in it. Please note
the id of the TextView itemTextView. We will need this id to set this TextView’s text with the item’s
string value.
item.xml
Step 4
Create a custom Adapter that extends RecyclerView.Adapter. This adapter takes care of populating
the items in it with values.
In Custom Adaptor class, we override three methods that are responsible for creating and setting values
for the items in RecyclerView.
1. onCreateViewHolder() creates the MyViewHolder widget for each item, with the specified
item.xml layout.
2. onBindViewHolder() gets the MyViewHolder widget for each item, and the position of the item as
arguments. Using the position, we can get the item from the items list. And we can use this item (String
in this case) to update MyViewHolder widget that is displayed as item (LinearLayout with TextView) in
the RecyclerView.
3. getItemCount() sets the number of elements to be displayed in the RecyclerView. We are setting this
size with the the size of ArrayList<String> itemsList in which we are storing the strings.
CustomAdapter.kt
package com.example.myapplication
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
Step 5
Inside MainActivity,
1. Get reference to the RecyclerView in layout file activity_main.xml. The id of the RecyclerView in layout
file is recyclerView.
2. Initialize CustomAdapter with the items List.
3. Set Layout Manager with LinearLayoutManager for the RecyclerView.
4. Set adapter with CustomAdapter for the RecyclerView.
5. Now we can prepare the items and update RecyclerView via CustomAdapter using
notifyDataSetChanged().
MainActivity.kt
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Step 6
Step 7
You may open Layout Inspector in Android Studio 4, Menu > View > Tool Windows > Layout
Inspector, and observe the items inside RecyclerView.