Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. getMainApp().API.getStates().enqueue(object : Callback<StateData>{
  2. override fun onFailure(call: Call<StateData>, t: Throwable) {
  3. Toast.makeText(this@SearchUserActivity, t.message, Toast.LENGTH_SHORT)
  4. }
  5.  
  6. override fun onResponse(call: Call<StateData>, response: Response<StateData>) {
  7. if (response.isSuccessful){
  8. val states = response.body()!!.data
  9. var stateArray = arrayListOf<String>()
  10. stateArray.add("Please Select State")
  11. for (state in states){
  12. stateArray.add(state.name)
  13. }
  14. val stateSpinner = findViewById<Spinner>(R.id.statespinner)
  15. val stateAdapter = ArrayAdapter<String>(this@SearchUserActivity, R.layout.customspinner, stateArray)
  16. stateSpinner.adapter = stateAdapter
  17. stateSpinner.setSelection(0)
  18. stateSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
  19. override fun onNothingSelected(parent: AdapterView<*>?) {
  20. Toast.makeText(this@SearchUserActivity, "Please Select State", Toast.LENGTH_SHORT).show()
  21. }
  22.  
  23. override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
  24. if (stateArray[position] != "Please Select State"){
  25. val stateid = states.get(position-1).id
  26. getDistrict(stateid)/*Here I am Passing id of State From Response of RetroFit.*/
  27. }else{
  28. Toast.makeText(this@SearchUserActivity, "Please Select State", Toast.LENGTH_SHORT).show()
  29. }
  30. }
  31.  
  32. }
  33. }
  34. }
  35.  
  36. })
  37. }
  38.  
  39. private fun getDistrict(stateid : Int){
  40. getMainApp().API.getDistricts(stateid).enqueue(object : Callback<DistrictData>{
  41. override fun onFailure(call: Call<DistrictData>, t: Throwable) {
  42. Toast.makeText(this@SearchUserActivity, t.message, Toast.LENGTH_SHORT)
  43. }
  44.  
  45. override fun onResponse(call: Call<DistrictData>, response: Response<DistrictData>) {
  46. if (response.isSuccessful){
  47. val districtSpinner = findViewById<Spinner>(R.id.districtspinner)
  48. val districtArray = arrayListOf<String>()
  49. districtArray.add("Please select District")
  50. val districts = response.body()!!.data
  51. for (district in districts) {
  52. districtArray.add(district.name)
  53. }
  54. val districtAdapter = ArrayAdapter<String>(this@SearchUserActivity, R.layout.customspinner, districtArray)
  55. districtSpinner.adapter = districtAdapter
  56. districtSpinner.setSelection(districtArray.indexOf("Please Select District"), true)
  57. districtSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
  58. override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
  59. if (districtArray[position] != "Please Select District"){
  60. Log.e("Districtid", districts.get(position).id.toString())/*I am Getting Nothing Here Just App gets Crash on This Link*/
  61. // val districtid = districts.get(position-1).id
  62. // getAC(districtid)
  63. }else{
  64. Toast.makeText(this@SearchUserActivity, "Please Select District", Toast.LENGTH_SHORT).show()
  65. }
  66. }
  67.  
  68. override fun onNothingSelected(parent: AdapterView<*>) {
  69. Toast.makeText(this@SearchUserActivity, "Please Select District", Toast.LENGTH_SHORT).show()
  70. }
  71. }
  72. }
  73. }
  74.  
  75. })
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement