改造响应问题

时间:2019-04-05 07:29:58

标签: android kotlin retrofit2 spinner

我正在尝试通过翻新获取状态列表,并尝试添加可搜索微调器。

我得到了什么:

我正在得到回应国家名单。

我可以在Spinner中访问一个状态。 Picture of Json 我想要什么:

我想在Spinner中访问多个状态。

代码:

public static void drawBlock (int sizeParam, int tierParam) {
        if (sizeParam == 3) {
            System.out.printf("%20s", "|..xxx..|\n");
        }
        else if (sizeParam == 5) {
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%29s", "|..xxx..|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%38s", "|........xxxxxxxxx........|\n");
            }
        }
        else if (sizeParam == 7) {
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%38s", "|..xxx..|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%47s", "|........xxxxxxxxx........|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%56s", "|..............xxxxxxxxxxxxxxx..............|\n");
            }
        }
        else if (sizeParam == 9) {
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%47s", "|..xxx..|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%56s", "|........xxxxxxxxx........|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%65s", "|..............xxxxxxxxxxxxxxx..............|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%74s", "|....................xxxxxxxxxxxxxxxxxxxxx....................|\n");
            }
        }
    }

    public static void drawTiers (int tiersParam) {
        for (int count = 0; count <= tiersParam/10; count++) {
            int size = tiersParam;
            drawBlock(size, tiersParam + 1);
    }
    }

Pokos:

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 = arrayListOf<String>(states?.get(0)!!.name)// Problem occur here i need to put value to get state
                    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
                        }
                    }
                }
            }

        })

3 个答案:

答案 0 :(得分:2)

此方法将使其简短地在数组中添加项目

new Container(
  decoration: BoxDecoration(
    color: Colors.grey.withOpacity(0.5),
    boxShadow: [
      BoxShadow(
        blurRadius: 5.0
      ),
    ],
  ),
),

答案 1 :(得分:1)

您可以使用这样的循环。

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
                        }
                    }
                }
            }

        })

在Kotlin中循环

for (i in 0..(response.body.data.size-1)) {
        stateArray.add(response.body.data.get(i).name)
    }

答案 2 :(得分:1)

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
                    }
                }
            }
    }
}
        }

    })

您只是访问列表上的第一个位置,因此微调框仅显示一个选项。

快乐编码...

相关问题