๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[Android] listView ๋งŒ๋“ค๊ธฐ (2023.07.14) ๋ณธ๋ฌธ

full stack/์•ˆ๋“œ๋กœ์ด๋“œ

[Android] listView ๋งŒ๋“ค๊ธฐ (2023.07.14)

Shinjio 2023. 7. 14. 17:44

 

package com.sjy.ex20230712

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView

class SubActivity : AppCompatActivity() {

    lateinit var btn_moveMain : Button
    lateinit var listview : ListView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub)

        btn_moveMain = findViewById(R.id.btn_moveMain)
        listview = findViewById(R.id.listview)

        btn_moveMain.setOnClickListener {
//            var it_moveMain : Intent = Intent(this, MainActivity::class.java)
//            startActivity(it_moveMain) //์ƒˆ๋กœ์šด Activity๋ฅผ Task์— ์Œ“๋Š” ๋ช…๋ น
                finish() //Activity ์‚ญ์ œ
        }

        //listview ๋งŒ๋“ค๊ธฐ
        //1. ๋ฐ์ดํ„ฐ (๋„์šฐ๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ)
        //2. ํ…œํ”Œ๋ฆฟ (ํ•œ ์ค„์— ์–ด๋–ค ๋ชจ์–‘์œผ๋กœ ํ‘œ์‹œํ•  ๊ฒƒ์ธ์ง€) - ํ…œํ”Œ๋ฆฟ ๋””์ž์ธ : Custom list view
        //    => android์—์„œ ์ œ๊ณตํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉ
        //3. Adapter (Data + ํ…œํ”Œ๋ฆฟ)
        //    => android์—์„œ ์ œ๊ณตํ•˜๋Š” adaptor ์‚ฌ์šฉ

        //1. ๋ฐ์ดํ„ฐ
        var foods = arrayOf("ํ–„๋ฒ„๊ฑฐ", "ํ”ผ์ž", "์น˜ํ‚จ", "๋ผˆํ•ด์žฅ๊ตญ", "๋‹ญ๋ฐœ")
        //2. ์–ด๋Œ‘ํ„ฐ ์ƒ์„ฑ
                                            //                  android ์ œ๊ณต ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ             ๋ฐ์ดํ„ฐ
        var adapter = ArrayAdapter<String>(applicationContext, android.R.layout.simple_list_item_1, foods )

        //3. listview์— adaptor ์—ฐ๊ฒฐ
        listview.adapter = adapter

    }
}