Skip to main content

How to populate a GridView with data in Kotlin Android

How to populate a GridView with data in Kotlin Android.

Here's a step-by-step tutorial on how to populate a GridView with data in Kotlin for Android.

Step 1: Create a new Android project​

To get started, open Android Studio and create a new Android project with an empty activity.

Step 2: Set up the layout​

Open the layout file for your activity (usually activity_main.xml) and add a GridView element. Give it an id for easier reference later.

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:padding="10dp"/>

Step 3: Create a model class​

Create a Kotlin data class to represent the data for each item in the GridView. For example, if you want to display images, you can create a class like this:

data class ImageItem(val imageResId: Int)

Step 4: Create the adapter​

Create a custom adapter class that extends the BaseAdapter class. This adapter will be responsible for binding the data to each item in the GridView.

class ImageAdapter(private val context: Context, private val items: List<ImageItem>) : BaseAdapter() {
override fun getCount(): Int {
return items.size
}

override fun getItem(position: Int): Any {
return items[position]
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
val viewHolder: ViewHolder

if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false)
viewHolder = ViewHolder(view)
view.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}

val item = items[position]
viewHolder.imageView.setImageResource(item.imageResId)

return view
}

private class ViewHolder(view: View) {
val imageView: ImageView = view.findViewById(R.id.imageView)
}
}

Step 5: Create the item layout​

Create a layout file (item_image.xml) for each item in the GridView. This layout will define the appearance of each item.

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>

Step 6: Set up the GridView in the activity​

In your activity's onCreate method, initialize the GridView and set the adapter to populate it with data.

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

val gridView: GridView = findViewById(R.id.gridView)
val imageItems = listOf(
ImageItem(R.drawable.image1),
ImageItem(R.drawable.image2),
ImageItem(R.drawable.image3),
// Add more items as needed
)
val adapter = ImageAdapter(this, imageItems)
gridView.adapter = adapter
}
}

Step 7: Run the app​

Build and run your app on an emulator or device. You should see the GridView populated with the images from the data source.

That's it! You have successfully populated a GridView with data in Kotlin for Android. You can customize the adapter and item layout to suit your specific needs, and add more functionality as required.