You are on page 1of 5

ListView In Android - Kotlin https://www.c-sharpcorner.

com/article/listview-in-android-kotlin/

01. <?xml version="1.0" encoding="utf-8"?>

1 of 5 18/06/2020 15:38
ListView In Android - Kotlin https://www.c-sharpcorner.com/article/listview-in-android-kotlin/

02. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


03. android:layout_width="match_parent"
04. android:layout_height="match_parent"
05. android:orientation="vertical">
06.
07. <ListView
08. android:id="@+id/listView"
09. android:layout_width="match_parent"
10. android:layout_height="match_parent" />
11. </LinearLayout>

01. <?xml version="1.0" encoding="utf-8"?>


02. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="match_parent"
04. android:layout_height="wrap_content"
05. android:focusable="true"
06. android:paddingLeft="16dp"
07. android:paddingRight="16dp"
08. android:paddingTop="10dp"
09. android:paddingBottom="10dp"
10. android:clickable="true"
11. android:background="?android:attr/selectableItemBackground"
12. android:orientation="vertical">
13.
14. <TextView
15. android:id="@+id/title"
16. android:textColor="@color/title"
17. android:textSize="16sp"
18. android:textStyle="bold"
19. android:layout_alignParentTop="true"
20. android:layout_width="match_parent"
21. android:layout_height="wrap_content" />
22.
23. <TextView
24. android:id="@+id/genre"
25. android:layout_below="@id/title"
26. android:layout_width="match_parent"
27. android:layout_height="wrap_content" />
28.
29. <TextView
30. android:id="@+id/year"
31. android:textColor="@color/year"
32. android:layout_width="wrap_content"
33. android:layout_alignParentRight="true"
34. android:layout_height="wrap_content" />
35.
36. </RelativeLayout>

01. class Movies {


02. var title: String = ""

2 of 5 18/06/2020 15:38
ListView In Android - Kotlin https://www.c-sharpcorner.com/article/listview-in-android-kotlin/

03. var genre: String = ""


04. var year: String = ""
05.
06. constructor() {}
07.
08. constructor(title: String, genre: String, year: String) {
09. this.title = title
10. this.genre = genre
11. this.year = year
12. }
13. }

01. class MoviesListViewAdapter(private val activity: Activity, moviesList: List) : BaseAdapter() {


02.
03. private var moviesList = ArrayList()
04.
05. init {
06. this.moviesList = moviesList as ArrayList
07. }
08.
09. override fun getCount(): Int {
10. return moviesList.size
11. }
12.
13. override fun getItem(i: Int): Any {
14. return i
15. }
16.
17. override fun getItemId(i: Int): Long {
18. return i.toLong()
19. }
20.
21. @SuppressLint("InflateParams", "ViewHolder")
22. override fun getView(i: Int, convertView: View?, viewGroup: ViewGroup): View {
23. var vi: View = convertView as View
24. val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
25. vi = inflater.inflate(R.layout.movie_list_row, null)
26. val title = vi.findViewById(R.id.title)
27. val genre = vi.findViewById(R.id.genre)
28. val year = vi.findViewById(R.id.year)
29. title.text = moviesList[i].title
30. genre.text = moviesList[i].genre
31. year.text = moviesList[i].year
32. return vi
33. }
34. }

01. listView = findViewById(R.id.listView) as ListView


02. adapter = MoviesListViewAdapter(this, movieList)
03. (listView as ListView).adapter = adapter

3 of 5 18/06/2020 15:38
ListView In Android - Kotlin https://www.c-sharpcorner.com/article/listview-in-android-kotlin/

01. class ListViewActivity : AppCompatActivity() {


02.
03. var listView: ListView? = null
04. var movieList = ArrayList<Movies>()
05. var adapter: MoviesListViewAdapter? = null
06.
07. override fun onCreate(savedInstanceState: Bundle?) {
08. super.onCreate(savedInstanceState)
09. setContentView(R.layout.activity_listview)
10.
11. listView = findViewById(R.id.listView) as ListView
12.
13. adapter = MoviesListViewAdapter(this, movieList)
14. (listView as ListView).adapter = adapter
15.
16. prepareMovieData()
17.
18. // Click event for single list row
19. (listView as ListView).onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->
Toast.makeText(applicationContext, movieList?.get(i)?.title, Toast.LENGTH_SHORT).show() }
20. }
21.
22. private fun prepareMovieData() {
23. var movie = Movies("Mad Max: Fury Road", "Action & Adventure", "2015")
24. movieList.add(movie)
25.
26. movie = Movies("Inside Out", "Animation, Kids & Family", "2015")
27. movieList.add(movie)
28.
29. movie = Movies("Star Wars: Episode VII - The Force Awakens", "Action", "2015")
30. movieList.add(movie)
31.
32. movie = Movies("Shaun the Sheep", "Animation", "2015")
33. movieList.add(movie)
34.
35. movie = Movies("The Martian", "Science Fiction & Fantasy", "2015")
36. movieList.add(movie)
37.
38. movie = Movies("Mission: Impossible Rogue Nation", "Action", "2015")
39. movieList.add(movie)
40.
41. movie = Movies("Up", "Animation", "2009")
42. movieList.add(movie)
43.
44. movie = Movies("Star Trek", "Science Fiction", "2009")
45. movieList.add(movie)
46.
47. movie = Movies("The LEGO Movies", "Animation", "2014")
48. movieList.add(movie)
49.
50. movie = Movies("Iron Man", "Action & Adventure", "2008")
51. movieList.add(movie)

4 of 5 18/06/2020 15:38
ListView In Android - Kotlin https://www.c-sharpcorner.com/article/listview-in-android-kotlin/

52.
53. movie = Movies("Aliens", "Science Fiction", "1986")
54. movieList.add(movie)
55.
56. movie = Movies("Chicken Run", "Animation", "2000")
57. movieList.add(movie)
58.
59. movie = Movies("Back to the Future", "Science Fiction", "1985")
60. movieList.add(movie)
61.
62. movie = Movies("Raiders of the Lost Ark", "Action & Adventure", "1981")
63. movieList.add(movie)
64.
65. movie = Movies("Goldfinger", "Action & Adventure", "1965")
66. movieList.add(movie)
67.
68. movie = Movies("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014")
69. movieList.add(movie)
70.
71. adapter?.notifyDataSetChanged()
72. }
73. }

5 of 5 18/06/2020 15:38

You might also like