如何在微调器中设置初始文本而不是 json 响应的第一项
How to set initial text in spinner instead first item of json response
我在我的应用程序中使用 json 解析,我从 json 获取国家名称,问题出在我的 json 响应中,第一个国家是 "Andorra",但默认情况下我想显示 "Select Country",以下是我的 json 响应和代码,有人可以帮助我吗??
[{"user_status":"1","country_id":"1","country":"Andorra"},{"user_status":"1","country_id":"2","country":"United Arab Emirates"},{"user_status":"1","country_id":"3","country":"Afghanistan"},{"user_status":"1","country_id":"4","country":"Antigua and Barbuda"},{"user_status":"1","country_id":"5","country":"Anguilla"},{"user_status":"1","country_id":"6","country":"Albania"},{"user_status":"1","country_id":"7","country":"Armenia"},{"user_status":"1","country_id":"8","country":"Angola"},]
我的代码
class Logincity extends
AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
private ProgressDialog pDialog;
private String test;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Registration.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(true);
pDialog.show();
}
protected ArrayList<HashMap<String, String>> doInBackground(
String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
jsonObjcitys = new JSONArray(jsonStr);
// state_list = jsonObj.getJSONArray(COUNTRY_LIST);
// looping through All Contacts
for (int i = 0; i < jsonObjcitys.length(); i++) {
JSONObject c = jsonObjcitys.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
//map.put(CITY_NAME, c.getString(CITY_NAME));
map.put(CITY_NAME, c.getString(CITY_NAME));
// map.put(PRESET_TITLES, c.getString(PRESET_TITLES));
citydata.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return citydata;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
arrallcitiies = new String[citydata.size()];
for (int index = 0; index < citydata.size(); index++) {
HashMap<String, String> map = citydata.get(index);
arrallcitiies[index] = map.get(CITY_NAME);
}
// pass arrConuntry array to ArrayAdapter<String> constroctor :
adapterallcities = new ArrayAdapter<String>(
Registration.this,
android.R.layout.simple_spinner_dropdown_item, arrallcitiies);
spinrcountry.setAdapter(adapterallcities);
spinrcountry.setPrompt("Select City");
spinrcountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
cityspitems = spinrcountry.getSelectedItem().toString();
System.out.println("PresetEVent selected" + cityspitems);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
只需要在该数组的第一个位置添加 "Select Country" 和 length=response items + 1
arrallcitiies = new String[citydata.size()+1];
arrallcitiies[0]="Select Country"; //item at pos 0
int j=1;
for (int index = 0; index < citydata.size(); index++) {
HashMap<String, String> map = citydata.get(index);
arrallcitiies[j++] = map.get(CITY_NAME);
}
adapterallcities = new ArrayAdapter<String>(Registration.this,
android.R.layout.simple_spinner_dropdown_item, arrallcitiies);
spinrcountry.setAdapter(adapterallcities);
您可以编辑
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
吼叫:
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map_select_country = new HashMap<String, String>();
map_select_country.put("0","Select country");
citydata.add(map_select_country); //add new this
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
尝试使用此代码
ArrayList<String> listcountry_id,listcountry;
listcountry_id=new ArrayList<String>();
listcountry=new ArrayList<String>();
try {
JSONArray jarr=new JSONArray("your responce data here");
for(int i=0;i<jarr.length();i++){
JSONObject jobj=jarr.getJSONObject(i);
listcountry_id.add(jobj.getString("country_id"));
listcountry.add(jobj.getString("country"));
}
BindSpinnerData();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
spnData.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String ids = String.valueOf(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
private void BindSpinnerData() {
// TODO Auto-generated method stub
ArrayAdapter<String> spinnerLocation = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinneritem, listcountry);
spinnerLocation.setDropDownViewResource(R.layout.spinnerpopup);
spnData.setAdapter(spinnerLocation);
}
//spinneritem,spinnerpopup 是textview布局,可以设置文字颜色和背景颜色
在 doInBackground()
中更改以下内容
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < jsonObjcitys.length(); i++) {
JSONObject c = jsonObjcitys.getJSONObject(i);
// creating new HashMap
// adding each child node to HashMap key => value
//map.put(CITY_NAME, c.getString(CITY_NAME));
map.put(CITY_NAME, c.getString(CITY_NAME));
// map.put(PRESET_TITLES, c.getString(PRESET_TITLES));
citydata.add(map);
}
map.put("Select_City", "Select City");
citydata.add(map);
在 onPostExecute()
中更改以下内容
设置微调器适配器后添加下面一行。
spinrcountry.setSelection(adapter.getCount());
您必须创建自定义适配器
public class HintAdapter extends ArrayAdapter < Objects > {
public HintAdapter(Context theContext, int theLayoutResId, String[] objects) {
super(theContext, theLayoutResId, R.id.text1, objects);
}
@Override
public int getCount() {
// don't display last item. It is used as hint.
int count = super.getCount();
return count > 0 ? count - 1 : count;
}
}
将 onPostExecute() 中的 setAdapter 行更改为以下-
adapterallcities = new HintAdapter(
Registration.this,
android.R.layout.simple_spinner_dropdown_item, arrallcitiies);
以上解决方案将显示默认 "Select City" 值,并且在微调器选择时将仅显示城市名称列表,没有默认值。
我在我的应用程序中使用 json 解析,我从 json 获取国家名称,问题出在我的 json 响应中,第一个国家是 "Andorra",但默认情况下我想显示 "Select Country",以下是我的 json 响应和代码,有人可以帮助我吗??
[{"user_status":"1","country_id":"1","country":"Andorra"},{"user_status":"1","country_id":"2","country":"United Arab Emirates"},{"user_status":"1","country_id":"3","country":"Afghanistan"},{"user_status":"1","country_id":"4","country":"Antigua and Barbuda"},{"user_status":"1","country_id":"5","country":"Anguilla"},{"user_status":"1","country_id":"6","country":"Albania"},{"user_status":"1","country_id":"7","country":"Armenia"},{"user_status":"1","country_id":"8","country":"Angola"},]
我的代码
class Logincity extends
AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
private ProgressDialog pDialog;
private String test;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Registration.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(true);
pDialog.show();
}
protected ArrayList<HashMap<String, String>> doInBackground(
String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
jsonObjcitys = new JSONArray(jsonStr);
// state_list = jsonObj.getJSONArray(COUNTRY_LIST);
// looping through All Contacts
for (int i = 0; i < jsonObjcitys.length(); i++) {
JSONObject c = jsonObjcitys.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
//map.put(CITY_NAME, c.getString(CITY_NAME));
map.put(CITY_NAME, c.getString(CITY_NAME));
// map.put(PRESET_TITLES, c.getString(PRESET_TITLES));
citydata.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return citydata;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
arrallcitiies = new String[citydata.size()];
for (int index = 0; index < citydata.size(); index++) {
HashMap<String, String> map = citydata.get(index);
arrallcitiies[index] = map.get(CITY_NAME);
}
// pass arrConuntry array to ArrayAdapter<String> constroctor :
adapterallcities = new ArrayAdapter<String>(
Registration.this,
android.R.layout.simple_spinner_dropdown_item, arrallcitiies);
spinrcountry.setAdapter(adapterallcities);
spinrcountry.setPrompt("Select City");
spinrcountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
cityspitems = spinrcountry.getSelectedItem().toString();
System.out.println("PresetEVent selected" + cityspitems);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
只需要在该数组的第一个位置添加 "Select Country" 和 length=response items + 1
arrallcitiies = new String[citydata.size()+1];
arrallcitiies[0]="Select Country"; //item at pos 0
int j=1;
for (int index = 0; index < citydata.size(); index++) {
HashMap<String, String> map = citydata.get(index);
arrallcitiies[j++] = map.get(CITY_NAME);
}
adapterallcities = new ArrayAdapter<String>(Registration.this,
android.R.layout.simple_spinner_dropdown_item, arrallcitiies);
spinrcountry.setAdapter(adapterallcities);
您可以编辑
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
吼叫:
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map_select_country = new HashMap<String, String>();
map_select_country.put("0","Select country");
citydata.add(map_select_country); //add new this
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
尝试使用此代码
ArrayList<String> listcountry_id,listcountry;
listcountry_id=new ArrayList<String>();
listcountry=new ArrayList<String>();
try {
JSONArray jarr=new JSONArray("your responce data here");
for(int i=0;i<jarr.length();i++){
JSONObject jobj=jarr.getJSONObject(i);
listcountry_id.add(jobj.getString("country_id"));
listcountry.add(jobj.getString("country"));
}
BindSpinnerData();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
spnData.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String ids = String.valueOf(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
private void BindSpinnerData() {
// TODO Auto-generated method stub
ArrayAdapter<String> spinnerLocation = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinneritem, listcountry);
spinnerLocation.setDropDownViewResource(R.layout.spinnerpopup);
spnData.setAdapter(spinnerLocation);
}
//spinneritem,spinnerpopup 是textview布局,可以设置文字颜色和背景颜色
在 doInBackground()
中更改以下内容HashMap<String, String> map = new HashMap<String, String>(); for (int i = 0; i < jsonObjcitys.length(); i++) { JSONObject c = jsonObjcitys.getJSONObject(i); // creating new HashMap // adding each child node to HashMap key => value //map.put(CITY_NAME, c.getString(CITY_NAME)); map.put(CITY_NAME, c.getString(CITY_NAME)); // map.put(PRESET_TITLES, c.getString(PRESET_TITLES)); citydata.add(map); } map.put("Select_City", "Select City"); citydata.add(map);
在 onPostExecute()
中更改以下内容设置微调器适配器后添加下面一行。
spinrcountry.setSelection(adapter.getCount());
您必须创建自定义适配器
public class HintAdapter extends ArrayAdapter < Objects > { public HintAdapter(Context theContext, int theLayoutResId, String[] objects) { super(theContext, theLayoutResId, R.id.text1, objects); } @Override public int getCount() { // don't display last item. It is used as hint. int count = super.getCount(); return count > 0 ? count - 1 : count; } }
将 onPostExecute() 中的 setAdapter 行更改为以下-
adapterallcities = new HintAdapter( Registration.this, android.R.layout.simple_spinner_dropdown_item, arrallcitiies);
以上解决方案将显示默认 "Select City" 值,并且在微调器选择时将仅显示城市名称列表,没有默认值。