Android

Android Studio_ 8. Spinner

아이른 2024. 3. 29. 14:07

활용 1. 사계절을 나타내는 스피너 만들기

 

  • adapter
    • listOf() : 스피너에 사용할 리스트 생성
    • ArrayAdapter() : 스피너에서 사용하는 어뎁터 (스피너에 들어갈 리스트를 넣기위한 중간 다리 역할)
      • (context, 스피너 목록이 가지는 레이아웃 파일, ArrayAdapter가 쓸 데이터)
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var data = listOf<String>("선택하세요", "봄", "여름", "가을", "겨울")
        var adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
        //android.R.layout.simple_list_item_1 : 안드로이드에서 만든 심플리스트
        binding.spinner.adapter = adapter
    }
}

 

활용 2. 스피너 목록을 선택하였을 때, textView에 선택 값 나타내기

  • onItemSelectedListener 
더보기

OnItemSelectedListener안에는 아래의 메서드들이 설계되어 있어 강제적으로 구현해야 함

    public interface OnItemSelectedListener {
        /**
         * <p>Callback method to be invoked when an item in this view has been
         * selected. This callback is invoked only when the newly selected
         * position is different from the previously selected position or if
         * there was no selected item.</p>
         *
         * Implementers can call getItemAtPosition(position) if they need to access the
         * data associated with the selected item.
         *
         * @param parent The AdapterView where the selection happened
         * @param view The view within the AdapterView that was clicked
         * @param position The position of the view in the adapter
         * @param id The row id of the item that is selected
         */
        void onItemSelected(AdapterView<?> parent, View view, int position, long id);

        /**
         * Callback method to be invoked when the selection disappears from this
         * view. The selection can disappear for instance when touch is activated
         * or when the adapter becomes empty.
         *
         * @param parent The AdapterView that now contains no selected item.
         */
        void onNothingSelected(AdapterView<?> parent);
    }
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var data = listOf<String>("선택하세요", "봄", "여름", "가을", "겨울")
        var adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
        
        with(binding) {
            spinner.adapter = adapter
            spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {//Ctrl+i
                override fun onItemSelected(parent: AdapterView<*>?,view: View?,position: Int,id: Long) {
                //선택했을 때 동작하는 메서드
                    val selected = data.get(position)//position : 스피너 리스트들의 선택 위치
                    result.text = selected
                }
                override fun onNothingSelected(parent: AdapterView<*>?) {
                //선택을 하지 않았을 때 동작하는 메서드
                }
            }
        }
    }
}

 

 

'Android' 카테고리의 다른 글

Android studio_ 0-1. Flow  (1) 2024.04.03
Android studio_ 1-2. Intent와 data class  (0) 2024.04.03
Android Studio_ 7-1. View binding  (0) 2024.03.28
Android Studio_ 7. View binding  (0) 2024.03.27
Android studio_ Values Resource File 사용  (0) 2024.03.26