class ListViewAdapter(private val items: MutableList<ListViewItem>): BaseAdapter() {
override fun getCount(): Int = items.size
override fun getItem(position: Int): ListViewItem = items[position]
override fun getItemId(position: Int): Long = position.toLong()
override fun getView(position: Int, view: View?, parent: ViewGroup?): View {
var convertView = view
if (convertView == null) convertView = LayoutInflater.from(parent?.context).inflate(R.layout.custom_list_item, parent, false)
val item: ListViewItem = items[position]
convertView!!.image_title.setImageDrawable(item.icon)
convertView.text_title.text = item.title
convertView.text_sub_title.text = item.SubTitle
return convertView
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image_title"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/text_title"
android:textSize="24dp"
android:textColor="#000000"
android:gravity="center_vertical"
android:layout_weight="2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/text_sub_title"
android:textSize="16dp"
android:textColor="#666666"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="주문하기"
android:textColor="@color/white"
android:background="#198CFF"
/>
<View
android:layout_width="10dp"
android:layout_height="0dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="용기정보"
android:textColor="@color/white"
android:background="#198CFF"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
class MainActivity : AppCompatActivity() {
val TAG: String ="로그"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val items = mutableListOf<ListViewItem>()
items.add(ListViewItem(ContextCompat.getDrawable(this,R.drawable.bibimbap)!!,"비빔밥","8000원"))
items.add(ListViewItem(ContextCompat.getDrawable(this,R.drawable.duengjangg)!!,"된장찌개","9000원"))
items.add(ListViewItem(ContextCompat.getDrawable(this,R.drawable.sundubuu)!!,"순두부찌개","9000원"))
items.add(ListViewItem(ContextCompat.getDrawable(this,R.drawable.kimchi)!!,"김치찌개","10000원"))
val adapter = ListViewAdapter(items)
listView.adapter = adapter
listView.setOnItemClickListener { parent: AdapterView<*>, view: View, position: Int, id: Long ->
val item = parent.getItemAtPosition(position) as ListViewItem
Toast.makeText(this, item.title, Toast.LENGTH_SHORT).show() }
}
}