-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListViewActivity.kt
88 lines (69 loc) · 2.69 KB
/
ListViewActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class ListViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_view)
val carList = ArrayList<CarForList>()
for (i in 0 until 50) {
carList.add(CarForList("" + i + " 번째 자동차", "" + i + "순위 엔진"))
}
val adapter = ListViewAdapter(carList, LayoutInflater.from(this@ListViewActivity))
val listview : ListView = findViewById(R.id.listview)
listview.adapter = adapter
listview.setOnItemClickListener { parent, view, position, id ->
val carName = (adapter.getItem(position) as CarForList).name
val carEngine = (adapter.getItem(position) as CarForList).engine
Toast.makeText(this@ListViewActivity, carName + "" + carEngine, Toast.LENGTH_SHORT ).show()
}
}
}
class ListViewAdapter(
val carForList : ArrayList<CarForList>,
val layoutInflater: LayoutInflater
) : BaseAdapter() {
override fun getCount(): Int {
// 그리고자 하는 아이템 리스트의 전체 갯수
return carForList.size
}
override fun getItem(position: Int): Any {
// 그리고자 하는 아이템 리스트의 하나 (포지션에 해당하는)
return carForList.get(position)
}
override fun getItemId(position: Int): Long {
// 해당 포지션에 위치해 있는 아이템뷰의 아이디 설정
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view : View
val holder : ViewHolder
if (convertView == null) {
Log.d("convert", "1")
view = layoutInflater.inflate(R.layout.item_view, null)
holder = ViewHolder()
holder.carName = view.findViewById(R.id.car_name)
holder.carEngine = view.findViewById(R.id.car_engine)
view.tag = holder
}else {
Log.d("convert", "2")
holder = convertView.tag as ViewHolder
view = convertView
}
holder.carName?.setText(carForList.get(position).name)
holder.carEngine?.setText(carForList.get(position).engine)
return view
}
}
class ViewHolder{
var carName : TextView? = null
var carEngine : TextView? = null
}