안드로이드

[Android Kotlin] 안드로이드 코틀린 Spinner

행복하개! 2020. 3. 24. 22:38
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="가져오기"
        android:id="@+id/btn"/>

</LinearLayout>
class MainActivity : AppCompatActivity() {

    var data1 = arrayOf("스피너1-1", "스피너1-2", "스피너1-3", "스피너1-4", "스피너1-5", "스피너1-6")
    var data2 = arrayOf("스피너2-1", "스피너2-2", "스피너2-3", "스피너2-4", "스피너2-5", "스피너2-6")

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

        var adapter1 = ArrayAdapter(this, android.R.layout.simple_spinner_item, data1)
        var adapter2 = ArrayAdapter(this, android.R.layout.simple_spinner_item, data2)

        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

        spinner.adapter = adapter1
        spinner2.adapter = adapter2

        var listener = SpinnerListener()

        spinner.onItemSelectedListener = listener

        spinner2.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                tv1.text = data2[p2]
            }

        }

        btn.setOnClickListener { view ->
            tv1.text = data1[spinner.selectedItemPosition]+"\n"
            tv1.append(data2[spinner2.selectedItemPosition])
        }
    }

    inner class SpinnerListener : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(p0: AdapterView<*>?) {

        }

        override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) { // p2가 사용자가 선택한 곳의 인덱스
            tv1.text = data1[p2]
        }
    }
}

 

 

 

우리가 웹 쇼핑몰에서 주문할 때, 물건을 선택할 때 많이보는 그 Spinner 이다. 아래 화살표 버튼을 누르면 쭉 내려가서 여러가지 항목들이 보이는 .. 

 

버튼에 리스너를 다는 것과 같이 다 비슷하다. 대신 OnItemSelectedListener의 오버라이딩 메서드가 하나가 아니기 때문에 람다식으로는 불가능하고 object에 상속시켜서 오버라이딩해서 사용하면 된다. onItemSelected에서 선택되었을 때 행위를 하면 된다. p2에 선택한 항목의 인덱스가 들어온다.

 

spinner의 인덱스를 가져오는 방법은 .selectedItemPosition으로 가져올 수 있다. 위에 버튼을 보면 이 필드를 읽어서 가져왔다.

 

궁금하신 점은 덧글로 남겨주세요.