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

JiYoung Dev ๐Ÿ–ฅ

[Android] intent & listView ์‹ฌํ™” (2023.07.18) ๋ณธ๋ฌธ

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

[Android] intent & listView ์‹ฌํ™” (2023.07.18)

Shinjio 2023. 7. 18. 18:20

Intent 

putExtra - intent

 

 

 

intent์— ๋ฐ์ดํ„ฐ ๋‹ด์•„์„œ ๋ณด๋‚ด๊ธฐ : putExtra

 

        btn_next.setOnClickListener {
            //Intent(ํ˜„์žฌActivicty, ๋„์ฐฉํ•˜๊ณ  ์‹ถ์€ Activicty)
            var inputEmail = edt_email.text.toString()
            var inputPw = edt_pw.text.toString()

            //1. id๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
            //  arrayList -> ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ™•์ธํ–ˆ์Œ
            //  HashMap -> containsKey()
            if(members.containsKey(inputEmail)){
                if(members.get(inputEmail).equals(inputPw)){
                    var it_next : Intent = Intent(this, SubActivity::class.java)
                    //intent์— ๊ฐ’ ๋‹ด์•„์ฃผ๊ธฐ
                    it_next.putExtra("email", inputEmail)
                    startActivity(it_next)
                }else{
                    Toast.makeText(applicationContext, "ํŒจ์Šค์›Œ๋“œ๊ฐ€ ํ‹€๋ฆฝ๋‹ˆ๋‹ค!", Toast.LENGTH_SHORT).show()
                }
            }else{
                Toast.makeText(applicationContext, "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์•„์ด๋””์ž…๋‹ˆ๋‹ค.", Toast.LENGTH_SHORT).show()
            }
        }

 

Intent๋กœ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ : intent

 

    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)

        //Intent๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ ๊บผ๋‚ด์„œ button์— ๋ผ์šฐ๊ธฐ
        //getIntent => intent
        var myIntent : Intent = intent
        //email์ด null์ผ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ? ํ‘œ์‹œ
        var email : String? = myIntent.getStringExtra("email")

        btn_moveMain.text = email + "๋‹˜ ์ข…๋ฃŒํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"

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

                finish() //Activity ์‚ญ์ œ
        }

 

 

registerForActivityResult

 

 

package com.sjy.firstapp

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.activity.result.contract.ActivityResultContracts

class MainActivity : AppCompatActivity() {

    lateinit var btn_bgcolor:Button

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

        btn_bgcolor = findViewById(R.id.btn_bgcolor)

        btn_bgcolor.setOnClickListener {
            //startActivity => ํŽธ๋„
            //ForResultLuancher => ์™•๋ณต
            var it : Intent = Intent(this, ColorActivity::class.java)
            //startActivity(it)
            frLauncher.launch(it)
        }
    }

    //Launcher ์„ค๊ณ„
    //                  resultLauncher ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ
    var frLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
        //it : activity ์ด๋™ํ–ˆ๋‹ค ๋Œ์•„์˜ฌ ๋•Œ ๋‹ด์•„์ค€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
        //๊ฐ”๋‹ค๊ฐ€ ๋Œ์•„์™”์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ๋ถ€๋ถ„
        //1. ์ด์ƒ ์—†์ด ์ž˜ ๋Œ์•„์™”๋Š”์ง€ ๊ฒ€์‚ฌ ์ง„ํ–‰
        if(it.resultCode == RESULT_OK){
            //2. ๋‹ด์•„์ค€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
            //  getIntExtra(key, defaultValue)
            // defaultValue => ํ‚ค๊ฐ’์ด ์—†์„ ๋•Œ
            val color = it.data!!.getIntExtra("color", -1) //!! : not null์ผ๋•Œ๋งŒ ์‹คํ–‰

            if(color == 0){
                btn_bgcolor.setBackgroundColor(Color.RED)
            }else if(color == 1){
                btn_bgcolor.setBackgroundColor(Color.GREEN)
            }else{
                btn_bgcolor.setBackgroundColor(Color.BLUE)
            }
        }
        //3.
    }
}

 

package com.sjy.firstapp

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

class ColorActivity : AppCompatActivity() {

    lateinit var lv_color:ListView

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

        lv_color = findViewById(R.id.lv_color)

        var colors = arrayOf("Red", "Green", "Blue")
        var adapter = ArrayAdapter<String>(applicationContext, android.R.layout.simple_list_item_1, colors)
        lv_color.adapter = adapter

        //setOnItemClickListener : listView์˜ ํ•ญ๋ชฉ ํด๋ฆญ ์ด๋ฒคํŠธ
        lv_color.setOnItemClickListener(AdapterView.OnItemClickListener { adapterView, view, i, l ->
            //1. ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ๋นˆ Intent ์ƒ์„ฑ
            var it : Intent = Intent() // ์™”๋‹ค๊ฐ€ ๋‹ค์‹œ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋นˆ Intent
            //2. Intent์— ๊ฐ’ ๋‹ด๊ธฐ(index)
            it.putExtra("color", i)
            //3. ๋‹ค์‹œ ๋Œ๋ ค์ฃผ๊ธฐ : setResult(์ฝ”๋“œ๊ฐ’, Intent)
            setResult(RESULT_OK, it)
            //4. ์ข…๋ฃŒ
            finish()
        })
    }
}

 

 

toString() ์จ์•ผ ํ•˜๋Š” ์ด์œ  

edt.text

=> char Sequence ํƒ€์ž…์ด๋ฏ€๋กœ equals ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ String์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผํ•จ

=> ๋”ฐ๋ผ์„œ toString()์„ ์จ์ค˜์•ผ ํ•œ๋‹ค. 

 

 

equals vs ==

equals๋Š” ๋ฐฐ์—ด ๋‚ด์— ๊ฐ’์„ ๋น„๊ต

== ๋Š” ์ฃผ์†Œ๊ฐ’์„ ๋น„๊ต

 

 

 

map ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์œ ๋ฆฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ

=> map์€ ๊ฒ€์ƒ‰์— ์šฉ์ด

 

์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•œ๊ฐ€?

Map vs List

 

List๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

1) ์ •๋ ฌํ•ด์•ผ๋จ

2) ๋ฐ์ดํ„ฐ๋“ค๋ผ๋ฆฌ ๋น„๊ตํ•ด์•ผ ํ•  ๋•Œ

3) ๋ฐ˜๋ณตํ•ด์•ผํ•  ๋•Œ

 

Map์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ 

1) ๊ฐ’์„ ๋ฝ‘์•„๋‚ด๊ณ  ์ฐพ์•„์•ผํ•  ๋•Œ

2) key-value ๋ฐ์ดํ„ฐ ํ˜•์‹์ผ ๋•Œ 

 

Hash Map 

: Map(์ธํ„ฐํŽ˜์ด์Šค) - ๊ฐ์ฒด ์ƒ์„ฑ ๋ถˆ๊ฐ€,  map์„ ์ƒ์†๋ฐ›์•„ ๋งŒ๋“  ๊ฒƒ

 

class MainActivity : AppCompatActivity() {

    lateinit var members : HashMap<String, String>

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

        //members = new HashMap() => HashMap ๊ฐ์ฒด ์ƒ์„ฑ
        members = HashMap()
        members.put("wldud", "0616")
        members.put("jiyoung", "6338")
        members.put("smhrd", "12345")
        members.put("K", "11111")
        members.put("sjy", "93")

 

 

ForResultLaucher

 

 

it๋Š” activityResult ํƒ€์ž…์œผ๋กœ ์•กํ‹ฐ๋น„ํ‹ฐ ์ด๋™ํ–ˆ๋‹ค ๋Œ์•„์˜ฌ ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ์Œ