Android Spinner 未更新 JSON 数据 Volley
Android Spinner not updating with JSON data Volley
对于以下代码,我使用 MultiSelectSpinner
所以我正在尝试使用 JSON 位置数据更新微调器,但由于某种原因它不会更新,即使我将它放在 onCreate 中也是如此。我知道我正在正确解析 JSON,因为当我要求 toast 打印出我得到的位置时,我正确地得到了每个位置。因此,我认为问题出在我创建 MultiSelectSpinner 并尝试设置项目时。但是,MultiSelectSpinner class 有一个用于设置带有字符串列表的项目的构造函数,所以我很困惑。我很想知道是否有人发现任何明显的错误或有任何建议。我也尝试过使用 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();
}
});
}
编辑:将我的 setItems(locationList) 移到我解析 onResponse 内部的数据后,它完美地工作了!
解析完所有数据后移动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();
}
对于以下代码,我使用 MultiSelectSpinner
所以我正在尝试使用 JSON 位置数据更新微调器,但由于某种原因它不会更新,即使我将它放在 onCreate 中也是如此。我知道我正在正确解析 JSON,因为当我要求 toast 打印出我得到的位置时,我正确地得到了每个位置。因此,我认为问题出在我创建 MultiSelectSpinner 并尝试设置项目时。但是,MultiSelectSpinner class 有一个用于设置带有字符串列表的项目的构造函数,所以我很困惑。我很想知道是否有人发现任何明显的错误或有任何建议。我也尝试过使用 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();
}
});
}
编辑:将我的 setItems(locationList) 移到我解析 onResponse 内部的数据后,它完美地工作了!
解析完所有数据后移动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();
}