Issue with Retrofit Response

前端 未结 3 1847
日久生厌
日久生厌 2021-01-20 15:09

I am trying to get List of States through Retrofit and trying to add in Searchable Spinner.

What I get :

I am getting List of States in Response.

I c

相关标签:
3条回答
  • 2021-01-20 15:27

    You can use a loop like this.

    getMainApp().electAPI.getStates().enqueue(object : Callback<Responseval>{
                override fun onFailure(call: Call<Responseval>, t: Throwable) {
                    Toast.makeText(this@MainActivity, t?.message, Toast.LENGTH_SHORT)
                }
    
                override fun onResponse(call: Call<Responseval>, response: Response<Responseval>) {
                    if (response.isSuccessful!!){
                        val states = response.body()?.data
                        var stateArray = arrayListOf<String>
                        for(i = 0; i<response.body.data.size; i++){
                            stateArray.add(response.body.data.get(i).name)
                        }
                        val spinner = findViewById<Spinner>(R.id.spinner)
                        val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, stateArray)
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                        spinner.adapter = adapter
                        val options = stateArray
                        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                                Toast.makeText(this@MainActivity, " You select >> "+options[position], Toast.LENGTH_SHORT).show();
                            }
    
                            override fun onNothingSelected(parent: AdapterView<*>) {
    
                                // sometimes you need nothing here
                            }
                        }
                    }
                }
    
            })
    

    for loop in kotlin

    for (i in 0..(response.body.data.size-1)) {
            stateArray.add(response.body.data.get(i).name)
        }
    
    0 讨论(0)
  • 2021-01-20 15:30

    This method will make it short add items in array

    getMainApp().electAPI.getStates().enqueue(object : Callback<Responseval>{
                override fun onFailure(call: Call<Responseval>, t: Throwable) {
                    Toast.makeText(this@MainActivity, t?.message, Toast.LENGTH_SHORT)
                }
    
                override fun onResponse(call: Call<Responseval>, response: Response<Responseval>) {
                    if (response.isSuccessful!!){
                        val states = response.body()?.data
                        var stateArray = arrayListOf<String>
                        for(state in states){
                            stateArray.add(states.get(i).name)
                        }
                        val spinner = findViewById<Spinner>(R.id.spinner)
                        val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, stateArray)
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                        spinner.adapter = adapter
                        val options = stateArray
                        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                                Toast.makeText(this@MainActivity, " You select >> "+options[position], Toast.LENGTH_SHORT).show();
                            }
    
                            override fun onNothingSelected(parent: AdapterView<*>) {
    
                                // sometimes you need nothing here
                            }
                        }
                    }
                }
    
            })
    
    0 讨论(0)
  • 2021-01-20 15:38
    getMainApp().electAPI.getStates().enqueue(object : Callback<Responseval>{
            override fun onFailure(call: Call<Responseval>, t: Throwable) {
                Toast.makeText(this@MainActivity, t?.message, Toast.LENGTH_SHORT)
            }
    
            override fun onResponse(call: Call<Responseval>, response: Response<Responseval>) {
                if (response.isSuccessful!!){
                    val states = response.body()?.data
                    val stateArray = Array<String>(states.size())
    for(int i=0;i<sates.size();i++)
    {
        stateArray[i]=sates.get(i).name;
    
        if(i == (sates.size() -1) )
        {
         val spinner = findViewById<Spinner>(R.id.spinner)
                    val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, stateArray)
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                    spinner.adapter = adapter
                    val options = stateArray
                    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                            Toast.makeText(this@MainActivity, " You select >> "+options[position], Toast.LENGTH_SHORT).show();
                        }
    
                        override fun onNothingSelected(parent: AdapterView<*>) {
    
                            // sometimes you need nothing here
                        }
                    }
                }
        }
    }
            }
    
        })
    

    You are just accessing the first position on the list that's why spinner is displaying only one option.

    Happy Coding...

    0 讨论(0)
提交回复
热议问题