【问题标题】:Android Spinner not updating with JSON data VolleyAndroid Spinner 不使用 JSON 数据更新 Volley
【发布时间】:2016-06-08 00:51:58
【问题描述】:

对于以下代码,我使用的是MultiSelectSpinner

所以我正在尝试使用 JSON 位置数据更新微调器,即使我将它放在 onCreate 中,它也不会由于某种原因更新。我知道我正在正确解析 JSON,因为当我要求 toast 打印出我得到的位置时,我正确地得到了每个位置。因此,我认为问题出在我创建 MultiSelectSpinner 并尝试 setItems 时。但是,MultiSelectSpinner 类有一个构造函数,用于设置带有字符串列表的项目,所以我很困惑。我很想知道是否有人发现任何明显的错误或有任何建议。我也尝试过使用 ArrayAdapter,但没有成功。

private List<String> locationList = new LinkedList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buyer_profile_edit);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

        RequestQueue mRequestQueue;
        Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024);

        Network network = new BasicNetwork(new HurlStack());
        final List<String> locationsProvided = new LinkedList<String>();
        mRequestQueue = new RequestQueue(cache, network);
        mRequestQueue.start();

        final String locationURL = "http://10.0.2.2:3000/apip/locations";
        // Testing for Valid JSON Web Token
        mRequestQueue.add(new JsonArrayRequest(locationURL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            for(int i = 0; i < response.length(); i++) {
                                JSONObject locObj = response.getJSONObject(i);
                                String location = locObj.getString("location");
                                locationList.add(location);
                                Toast.makeText(BuyerProfileEdit.this,location,Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }));

        MultiSelectSpinner mySpin = (MultiSelectSpinner)findViewById(R.id.locationDropDownBuyer);
        mySpin.setItems(locationList);


        Button saveProfile =  (Button) findViewById(R.id.submitProfile);

        saveProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBuyerProfile();
            }
        });


    }

编辑:在我解析 onResponse 中的数据后将我的 setItems(locationList) 移动到右侧后,它可以完美运行!

【问题讨论】:

    标签: android json spinner android-volley


    【解决方案1】:

    在解析完所有数据后移动setItems

    try {
                                for(int i = 0; i < response.length(); i++) {
                                    JSONObject locObj = response.getJSONObject(i);
                                    String location = locObj.getString("location");
                                    locationList.add(location);
                                    Toast.makeText(BuyerProfileEdit.this,location,Toast.LENGTH_LONG).show();
                                }
                               MultiSelectSpinner mySpin = (MultiSelectSpinner)findViewById(R.id.locationDropDownBuyer);
            mySpin.setItems(locationList);
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
    

    【讨论】:

    • 是的!谢谢你。卡在这上面一天了。现在可以完美运行。
    • 当然!在 Stackoverflow 允许我标记它之前,我不得不稍等片刻,但我只是做了。再次感谢您!
    • @DavidParks 祝你未来好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多