FYI I'm starting to learn Kotlin & have previous experience with Python and PyQT.

Goal is to have a for loop iterate a certain amount of times based on user input. Each iteration of the for loop should bring up an alert dialog that saves the text input to ArrayList.

Currently, an alert dialog shows asking for the site name. After entering the name and tapping 'Ok' an alert dialog shows asking for the amount of buildings at the site. That input gets saved as an integer used as the for loop limit. This is when the app crashes. The alert dialog asking for the site name never shows. Any suggestions?

btn_new_site.setOnClickListener { view ->
        //maybe convert this to seperate .kt and import in.
        //ask for building names
        val building_name_builder = AlertDialog.Builder(this)
        val buiding_name_inflater = layoutInflater
        building_name_builder.setTitle("Building Name")
        val dialogLayout2 = 
           buiding_name_inflater.inflate(R.layout.ad_building_name, null)
        val building_name  = dialogLayout2.findViewById<EditText> 
           (R.id.site_building_name)
        building_name_builder.setView(dialogLayout2)
        building_name_builder.setPositiveButton("OK") { dialogInterface, 
        i ->
            //add to list of buildings for x site here
        }

        //ask for amount of buildings at site
        val amt_building_builder = AlertDialog.Builder(this)
        val amt_building_inflater = layoutInflater
        amt_building_builder.setTitle("How many buildings?")
        val dialogLayout1 = 
            amt_building_inflater.inflate(R.layout.ad_site_amt_buildings, null)
        val site_amtBuildings  = dialogLayout1.findViewById<EditText> 
           (R.id.site_amtBuildings)
        amt_building_builder.setView(dialogLayout1)
        amt_building_builder.setPositiveButton("OK") { _, i ->
            val amt_buildings = site_amt_buildings.text.toString().toInt()
            for ( buildings in 1..amt_buildings)
                //ask for building names here
                building_name_builder.show()
        }

        //ask for site name
        val site_builder = AlertDialog.Builder(this)
        val site_inflater = layoutInflater
        site_builder.setTitle("Site Name")
        val dialogLayout = site_inflater.inflate(R.layout.ad_site_name, null)
        val site_name  = dialogLayout.findViewById<EditText> 
           (R.id.site_name)
        site_builder.setView(dialogLayout)
        site_builder.setPositiveButton("OK") { dialogInterface, i ->
            sites.add(site_name.text.toString())
            amt_building_builder.show()
        }

        site_builder.show()

    }

No errors show when compiling. Just warning about some parameters never being used.

0 Answers