[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-3

2022. 6. 8. 21:07·안드로이드 프로그래밍/코틀린
반응형

Spinner의 onItemSelectedListener에 AdapterView.OnitemSelectedListener 람다식을 대입하는 방법은

Object 객체를 만들어서 onItemSelected, onNothingSelected 을 오버라이딩하면 됩니다.

spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                ivPoster.setImageResource(posterID[position])
            }
 
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
 

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

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
package com.cookandroid.project11_3
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "스피너 테스트"
 
        var movie = arrayOf(
            "영화 제목1", "영화 제목2", "영화 제목3", "영화 제목4",
            "영화 제목5", "영화 제목6", "영화 제목7", "영화 제목8",
            "영화 제목9", "영화 제목10"
        )
 
        var posterID = arrayOf(
            R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04,
            R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
            R.drawable.mov09, R.drawable.mov10,
        )
 
        var adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, movie)
        spinner1.adapter = adapter
        spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                ivPoster.setImageResource(posterID[position])
            }
 
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
 
        }
    }
}
Colored by Color Scripter
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner1"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:id="@+id/ivPoster"/>
 
</LinearLayout>
Colored by Color Scripter
cs
반응형
저작자표시 (새창열림)

'안드로이드 프로그래밍 > 코틀린' 카테고리의 다른 글

[코틀린을 활용한 안드로이드 프로그래밍] 12장 직접 풀어보기 12-2  (0) 2022.06.09
[코틀린 안드로이드 프로그래밍] Spinner OnItemSelectedListener사용법  (0) 2022.06.08
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-2  (0) 2022.06.08
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-1  (0) 2022.06.08
[코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-3  (0) 2022.05.26
'안드로이드 프로그래밍/코틀린' 카테고리의 다른 글
  • [코틀린을 활용한 안드로이드 프로그래밍] 12장 직접 풀어보기 12-2
  • [코틀린 안드로이드 프로그래밍] Spinner OnItemSelectedListener사용법
  • [코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-2
  • [코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-1
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (198)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (0)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    그리디
    시뮬레이션
    백준
    BFS
    코틀린
    구현
    우선순위 큐
    C
    알고리즘
    코틀린을 활용한 안드로이드 프로그래밍
    C++
    dfs
    백트랙킹
    Kotlin
    콘솔 테트리스 게임
    다이나믹 프로그래밍
    자료구조
    그래프
    dp
    콘솔
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-3
상단으로

티스토리툴바