Java 用自定义对象填充微调器。如何在自定义对象中设置微调器提示
我正在开发一个应用程序,其中有一个微调器。我正在微调器中解析JSON,并使用自定义对象设置适配器。现在我遇到了一个无法设置提示的问题。正如图中第一个国家来到阿富汗,但它应该像“选择国家”一样暗示。请帮帮我,我两天来一直困在这里。提前谢谢 列出mCountryList 私人国家({ ApiService countryApi=conversionclient.getApiService()Java 用自定义对象填充微调器。如何在自定义对象中设置微调器提示,java,android,Java,Android,我正在开发一个应用程序,其中有一个微调器。我正在微调器中解析JSON,并使用自定义对象设置适配器。现在我遇到了一个无法设置提示的问题。正如图中第一个国家来到阿富汗,但它应该像“选择国家”一样暗示。请帮帮我,我两天来一直困在这里。提前谢谢 列出mCountryList 私人国家({ ApiService countryApi=conversionclient.getApiService() Call Call=countryApi.getCountry(); mCountryList=新的Arra
Call Call=countryApi.getCountry();
mCountryList=新的ArrayList();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
试一试{
JSONObject对象=新的JSONObject(response.body().toString());
JSONObject jsonObj=新的JSONObject(object.toString());
JSONArray JSONArray=jsonObj.getJSONArray(“数据”);
if(jsonArray.length()>0){
for(int i=0;i
一种简单的方法是在开始时创建一个虚拟国家对象,并将其添加到名为选择国家
的数组中,并填写您可能需要的其他虚拟详细信息。当点击任何表单按钮(假设您有一个)时,您可以检查当前选择的索引,如果它是0
,则不选择任何国家。那样的话,你可以举杯祝酒
例如,下面是更新的try-catch块:
try {
JSONObject object = new JSONObject(response.body().toString());
JSONObject jsonObj = new JSONObject(object.toString());
JSONArray jsonArray = jsonObj.getJSONArray("data");
if (jsonArray.length() > 0) {
Country country = new Country();
country.setId(0);
country.setName("Select Country");
country.setCode(0);
mCountryList.add(country);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Country country = new Country();
country.setId(jsonObject.getInt("id"));
country.setName(jsonObject.getString("name"));
country.setCode(jsonObject.getString("code"));
mCountryList.add(country);
}
ArrayAdapter < Country > arrayAdapter = new ArrayAdapter < Country > (getApplicationContext(), R.layout.spinner_layout, country);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerCountry.setAdapter(arrayAdapter);
}
} catch (Exception e) {
e.printStackTrace();
}
试试看{
JSONObject对象=新的JSONObject(response.body().toString());
JSONObject jsonObj=新的JSONObject(object.toString());
JSONArray JSONArray=jsonObj.getJSONArray(“数据”);
if(jsonArray.length()>0){
国家=新国家();
国家。setId(0);
country.setName(“选择国家”);
国家设置码(0);
McCountryList.add(国家);
for(int i=0;iArrayAdapter=新的ArrayAdapter(getApplicationContext(),R.layout.spinner\u layout,Country);
arrayAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
mspinneCountry.setAdapter(arrayAdapter);
}
}捕获(例外e){
e、 printStackTrace();
}
第一个元素是您的提示,确保您在实际想要采取任何行动时检查索引,例如,如果索引为
0
向用户敬酒以选择国家。您能在这里写下我可以轻松理解的伪代码吗?太好了。快乐编码。提示来自微调器项目。我们可以从列表中删除仅作为提示显示的内容吗?好的,非常感谢您的建议。
try {
JSONObject object = new JSONObject(response.body().toString());
JSONObject jsonObj = new JSONObject(object.toString());
JSONArray jsonArray = jsonObj.getJSONArray("data");
if (jsonArray.length() > 0) {
Country country = new Country();
country.setId(0);
country.setName("Select Country");
country.setCode(0);
mCountryList.add(country);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Country country = new Country();
country.setId(jsonObject.getInt("id"));
country.setName(jsonObject.getString("name"));
country.setCode(jsonObject.getString("code"));
mCountryList.add(country);
}
ArrayAdapter < Country > arrayAdapter = new ArrayAdapter < Country > (getApplicationContext(), R.layout.spinner_layout, country);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerCountry.setAdapter(arrayAdapter);
}
} catch (Exception e) {
e.printStackTrace();
}