0

I write a kotlin app and wonder, why the code runs through the listener during initializing it. I try to explain the question with my code:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        findpath() 
        //check, if Dateien existieren, sonst create in function
        checkOrCreateFiles()

        binding = ActivityMainBinding.inflate(layoutInflater)
        var view = binding.root
        setContentView(view) //R.layout.activity_main)
        val cadapterk: ArrayAdapter<String> = ArrayAdapter<String>(
            this,
            android.R.layout.simple_spinner_item, myvokdirs
        )

        binding.spinnerKasten.adapter = cadapterk
        //binding.spinnerKasten.setSelection(0)  
        binding.spinnerKasten.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>,
                view: View,
                position: Int,
                id: Long
            ) {
                kastenselected = myvokdirs[position].toString()
                setnewpath(kastenselected)
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                kastenselected = myvokdirs[0]   
                
            }
        }

        binding.AuswahlContainer.isEnabled = false
        fileAktuell = boxes[0]   //dateiAkt
        checkAktuell = gut[0] // gutAkt
        readDatei(fileAktuell)
        binding.spinnerKasten.isEnabled = false
        // some addional code
    }

The situation / problem

In principle, the code works. Binding is o.k. The spinner "binding.spinnerkasten" is o.k. The associated adapter "cadapterk" is ok and shows data of my list "myvokdirs". BUT:

during initializing the spinner the code runs through "setnewpath". But "setnewpath" should be used only after selecting an item in the spinner.

How can I avoid, that "setnewpath" is fired during init? It seems, that the app runs through the listener during onCreate-function.

What is wrong or what is my misunderstanding, that the code fires "setnewpath" already in init instead of only after selecting an item?

do I habe to combine it with an onCLickListener?

(All other things are correct. The spinner appears on the right place, the spinner shows the correct data

user3367867
  • 565
  • 1
  • 7
  • 19
  • https://stackoverflow.com/questions/2562248/how-to-keep-onitemselected-from-firing-off-on-a-newly-instantiated-spinner – Dinkar Kumar May 03 '21 at 20:16

1 Answers1

2

onItemSelected is always called upon loading if you dont want something to run on initial load then surround with a boolean

var firstLoad = true

override fun onItemSelected(
            parent: AdapterView<*>,
            view: View,
            position: Int,
            id: Long
        ) {
            if(!firstLoad){
                kastenselected = myvokdirs[position].toString()
                setnewpath(kastenselected)
            }else{
                firstLoad = false
            }
        }
tyczj
  • 71,600
  • 54
  • 194
  • 296