单击时填充微调器

发布于 2025-01-11 13:23:39 字数 2114 浏览 2 评论 0原文

我正在尝试这个简单的程序,我尝试向服务器发送 POST 请求并获取响应并将它们存储到 spinner 中。这是我的代码;

    fetchcolonynames("code");  //sending POST request
    if (spin1 != null) {
        //spin.setAdapter(adapter) doesn't work here
        spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                spin1.setAdapter(adapter); //doesn't work spinner shows nothing on clicked
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

这是我的 fetchcolonynames() 方法:

private void fetchcolonynames(String input){
        call=api.fetchcolonynames(input);
        if(call!=null){
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.body() != null) {
                        JSONArray array = null;
                        try {
                            array = new JSONArray(response.body());
                            for(int i=0;i<array.length();i++){
                                arr.add(array.getString(i));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Collections.sort(arr); //data is correct here
                        adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_item,arr);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {

                }
            });
        }
    }

我的所有变量都是全局声明的。我想在用户单击时填充spinner。我该如何实现这个目标?

I am trying out this simple program where I try to send a POST request to the server and fetch the response and store them into the spinner. This is my code;

    fetchcolonynames("code");  //sending POST request
    if (spin1 != null) {
        //spin.setAdapter(adapter) doesn't work here
        spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                spin1.setAdapter(adapter); //doesn't work spinner shows nothing on clicked
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

This is my fetchcolonynames() method:

private void fetchcolonynames(String input){
        call=api.fetchcolonynames(input);
        if(call!=null){
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.body() != null) {
                        JSONArray array = null;
                        try {
                            array = new JSONArray(response.body());
                            for(int i=0;i<array.length();i++){
                                arr.add(array.getString(i));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Collections.sort(arr); //data is correct here
                        adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_item,arr);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {

                }
            });
        }
    }

All my variables are globally declared. I want to populate the spinner, when the user clicks it. How do I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文