在android中没有将服务器项目放入我的微调器中?
我想从服务器获取数据到我的微调,但它没有显示任何项目在微调,我也没有得到任何logcat错误也..我采取了这个从1个例子,在我的JSON输出,我只想抓取国家的名字..但它不显示任何:在android中没有将服务器项目放入我的微调器中?
这是我的java类:
pmcountry = (Spinner) findViewById(R.id.country);
//citySpinner = (Spinner) findViewById(City);
//locationSpinner = (Spinner) findViewById(R.id.Location);
pmcountry .setOnItemSelectedListener(this);
country_list = new ArrayList<String>();
//location_list = new ArrayList<String>();
// city_list = new ArrayList<String>();
getData();
}
private void getData(){
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
Log.d("Test",response);
JSONArray result = new JSONArray(response);
//Calling method getCountry to get the Country from the JSON Array
getCountry(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}});
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getCountry(JSONArray jsonArrayCountry){
//Traversing through all the items in the json array
List<Country> countries = new ArrayList<>();
try {
String country_name, country_code;
JSONObject countries_object;
for (int i = 0; i < jsonArrayCountry.length(); i++) {
countries_object = jsonArrayCountry.getJSONObject(i);
country_code = countries_object.getString("id");
country_name = countries_object.getString("Name");
countries.add(new Country(country_code, country_name));
}
/*ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
pmcountry.setPrompt("--Select Country--");
pmcountry.setAdapter(countryAdapter);
pmcountry.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter,
R.layout.contact_spinner_row_nothing_selected,this));*/
pmcountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
} catch (JSONException e) {
}
}
您试图设置自定义的对象列表List<Country>
通过默认数组适配器使用String的列表。 List<String>
。
您必须调整您的代码以设置微调器中的自定义对象列表。
Android: How to bind spinner to custom object list?
这应该解决您的问题。
pmcountry.setAdapter(countryAdapter); pmcountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
使用此从服务器获取的所有数据之后
我面临的dropdownview资源 – user7316606
错误它说无法解决梅索德 – user7316606
取消注释你的,你是不是设定数据在任何地方微调器..
ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
pmcountry.setPrompt("--Select Country--");
pmcountry.setAdapter(countryAdapter);
pmcountry.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter,
R.layout.contact_spinner_row_nothing_selected,this));
,但它的工作在我的另一个项目中,我只是试着用这个JSON输出.1更多的东西在输出中只有名称给出..而我尝试使用ID也..如果我尝试删除country_code ..显示错误 – user7316606