Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์ค๋ธ์
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- Java
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- K๋ฐฐํฐ๋ฆฌ
- ์ํ
- database
- ComputerScience
- ๋ ์
- ๋ผํ๋ผ์ค์๋ง๋
- ์๋ฐ
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- Python
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- JavaScript
- ์๋ฐ์คํฌ๋ฆฝํธ
- ํ๋ก๊ทธ๋๋ฐ
- html
- ์ฝ๋ฉ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ์ปดํจํฐ๊ณผํ
- ํ์ด์ฌ
- ๋ฐ์ํ
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- ์นํผ๋ธ๋ฆฌ์ฑ
- css
- ๋ฆฌ์กํธ
- ์ฑ
- ์ค๋ผํด
- ๊ฐ๋ฐ
Archives
- Today
- Total
JiYoung Dev ๐ฅ
[Android] intent & listView ์ฌํ (2023.07.18) ๋ณธ๋ฌธ
full stack/์๋๋ก์ด๋
[Android] intent & listView ์ฌํ (2023.07.18)
Shinjio 2023. 7. 18. 18:20Intent
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 ํ์ ์ผ๋ก ์กํฐ๋นํฐ ์ด๋ํ๋ค ๋์์ฌ ๋ ๋ฐ์ดํฐ๋ฅผ ๋ด๊ณ ์์
'full stack > ์๋๋ก์ด๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] JSON, GSON (2023.07.19) (0) | 2023.07.19 |
---|---|
[Android] POST ๋ฐฉ์ ํต์ (2023.07.19~20) (0) | 2023.07.19 |
[Android] HTTP ํต์ - Volley (2023.07.18) (0) | 2023.07.18 |
[Android] listView ๋ง๋ค๊ธฐ (2023.07.14) (0) | 2023.07.14 |
[Android] intent (2023.07.14) (0) | 2023.07.14 |