fetch data for spinners from REST APi [cascade]

145 views Asked by At

I have in one activity 6 spinners and at the beginnig to 1st one it fetchs data automatically using rest api and retrofit and the for another spinner i need use value which was selected from 1st spinner and fo the 3d spinner i need get selectedItem from second spinner and so on.

i've allready (for fast result) try this in my Activity class:

 private fun buildCountryDropDown(order: Int, objectName: Spinner?) {
    try {

        var rooms: Call<Created>? = null

        var selectedObject: String? = null

        val data = jsonApi.getObjects()

        data?.enqueue(object : Callback<Created> {
            override fun onResponse(call: Call<Created>, response: Response<Created>) {
                if (response.isSuccessful) {
                    listOfDataObjects = response.body()!!.data.toMutableList()
                    println("text==========: " + response.body()?.data?.get(0)?.NameRu)

                    val cAdapter = CreatedAdapter(this@PlanJobActivity, android.R.layout.simple_spinner_item, listOfDataObjects as ArrayList<Data>?)
                    objectName?.adapter = cAdapter
                }
            }

            override fun onFailure(call: Call<Created>?, t: Throwable?) {
                t?.printStackTrace()
            }
        })

        objectName?.onItemSelectedListener = object : OnItemSelectedListener {

            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                selectedObject = listOfDataObjects[position].tid

                rooms = jsonApi.getRooms(selectedObject!!)

                showToast("$selectedObject was selected!")
            }

            override fun onNothingSelected(parent: AdapterView<*>) {}
        }
        //=============================================ROOMS============================

        rooms?.enqueue(object : Callback<Created> {
            override fun onResponse(call: Call<Created>, response: Response<Created>) {
                if (response.isSuccessful) {
                    listOfRooms = response.body()!!.data.toMutableList()
                    println("text==========: " + response.body()?.data?.get(0)?.NameRu)

                    val cAdapter = CreatedAdapter(this@PlanJobActivity, android.R.layout.simple_spinner_item, listOfRooms as ArrayList<Data>?)
                    vnp?.adapter = cAdapter
                }
            }

            override fun onFailure(call: Call<Created>?, t: Throwable?) {
                t?.printStackTrace()
            }
        })

        vnp?.onItemSelectedListener = object : OnItemSelectedListener {

            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                selectedObject = listOfRooms[position].tid

                showToast("$selectedObject was selected!")
            }

            override fun onNothingSelected(parent: AdapterView<*>) {}
        }

    } catch (e: Exception) {
        e.printStackTrace()
    }
}

I know it is very very bad idea to write logic in activity but i just try, but not works. it fetch data only for first spinner but other spinners stay empty. For fetching data from rest api i'm using a retrofit and how can you see i get data from responce by enqueue method witch allows me to fetch data async. I understand that i need remaster my project to MVP but i dont know how to start?

0

There are 0 answers