2016-06-07 43 views
2

更新对于下面的代码我使用MultiSelectSpinnerAndroid的微调不是JSON数据凌空

所以我试图更新JSON位置数据的微调,它不会更新由于某种原因,即使我把它里面的onCreate。我知道我正确地解析了JSON,因为当我要求敬酒打印出我得到的位置时,我正确地获取了每个位置。因此,我认为问题出在我创建MultiSelectSpinner并尝试setItems时。然而,MultiSelectSpinner类有一个构造函数用于设置带有String列表的项目,所以我很困惑。我很想知道是否有人发现任何明显的错误或有任何建议。我也试图使用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)移动到了正确位置后,它完美地工作!

回答

0

移动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(); 
         } 
+0

是后!谢谢。一直坚持这一点。现在工作完美无瑕。当然是 –

+0

!在Stackoverflow允许我标记它之前,必须等一会儿,但我只是做了。再一次感谢你! –

+0

@DavidParks未来的好运 –