Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getMainApp().API.getStates().enqueue(object : Callback<StateData>{
- override fun onFailure(call: Call<StateData>, t: Throwable) {
- Toast.makeText(this@SearchUserActivity, t.message, Toast.LENGTH_SHORT)
- }
- override fun onResponse(call: Call<StateData>, response: Response<StateData>) {
- if (response.isSuccessful){
- val states = response.body()!!.data
- var stateArray = arrayListOf<String>()
- stateArray.add("Please Select State")
- for (state in states){
- stateArray.add(state.name)
- }
- val stateSpinner = findViewById<Spinner>(R.id.statespinner)
- val stateAdapter = ArrayAdapter<String>(this@SearchUserActivity, R.layout.customspinner, stateArray)
- stateSpinner.adapter = stateAdapter
- stateSpinner.setSelection(0)
- stateSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
- override fun onNothingSelected(parent: AdapterView<*>?) {
- Toast.makeText(this@SearchUserActivity, "Please Select State", Toast.LENGTH_SHORT).show()
- }
- override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
- if (stateArray[position] != "Please Select State"){
- val stateid = states.get(position-1).id
- getDistrict(stateid)/*Here I am Passing id of State From Response of RetroFit.*/
- }else{
- Toast.makeText(this@SearchUserActivity, "Please Select State", Toast.LENGTH_SHORT).show()
- }
- }
- }
- }
- }
- })
- }
- private fun getDistrict(stateid : Int){
- getMainApp().API.getDistricts(stateid).enqueue(object : Callback<DistrictData>{
- override fun onFailure(call: Call<DistrictData>, t: Throwable) {
- Toast.makeText(this@SearchUserActivity, t.message, Toast.LENGTH_SHORT)
- }
- override fun onResponse(call: Call<DistrictData>, response: Response<DistrictData>) {
- if (response.isSuccessful){
- val districtSpinner = findViewById<Spinner>(R.id.districtspinner)
- val districtArray = arrayListOf<String>()
- districtArray.add("Please select District")
- val districts = response.body()!!.data
- for (district in districts) {
- districtArray.add(district.name)
- }
- val districtAdapter = ArrayAdapter<String>(this@SearchUserActivity, R.layout.customspinner, districtArray)
- districtSpinner.adapter = districtAdapter
- districtSpinner.setSelection(districtArray.indexOf("Please Select District"), true)
- districtSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
- override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
- if (districtArray[position] != "Please Select District"){
- Log.e("Districtid", districts.get(position).id.toString())/*I am Getting Nothing Here Just App gets Crash on This Link*/
- // val districtid = districts.get(position-1).id
- // getAC(districtid)
- }else{
- Toast.makeText(this@SearchUserActivity, "Please Select District", Toast.LENGTH_SHORT).show()
- }
- }
- override fun onNothingSelected(parent: AdapterView<*>) {
- Toast.makeText(this@SearchUserActivity, "Please Select District", Toast.LENGTH_SHORT).show()
- }
- }
- }
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement