Android 如何在微调器中设置初始文本,而不是json响应的第一项
我在我的应用程序中使用json解析,我从json中获取国家名称,问题在于我的json响应中第一个国家是“安道尔”,但默认情况下我想显示“选择国家”,下面是我的json响应和代码,有人能帮我吗Android 如何在微调器中设置初始文本,而不是json响应的第一项,android,json,android-spinner,Android,Json,Android Spinner,我在我的应用程序中使用json解析,我从json中获取国家名称,问题在于我的json响应中第一个国家是“安道尔”,但默认情况下我想显示“选择国家”,下面是我的json响应和代码,有人能帮我吗 [{"user_status":"1","country_id":"1","country":"Andorra"},{"user_status":"1","country_id":"2","country":"United Arab Emirates"},{"user_status":"1","countr
[{"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) {
}
});
}
}
类登录扩展
异步任务{
私人对话;
私有字符串测试;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Registration.this);
pDialog.setMessage(“请稍候…”);
pDialog.setUndeterminate(真);
pDialog.setIndenderedRavable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(真);
pDialog.show();
}
受保护的ArrayList doInBackground(
字符串…args){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
citydata=newarraylist();
字符串jsonStr=sh.makeServiceCall(CITY\uURL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
jsonObjcitys=新的JSONArray(jsonStr);
//state_list=jsonObj.getJSONArray(COUNTRY_list);
//通过所有触点循环
对于(int i=0;ivalue
//map.put(城市名称,c.getString(城市名称));
map.put(城市名称,c.getString(城市名称));
//map.put(预设标题,c.getString(预设标题));
城市数据。添加(地图);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回城市数据;
}
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
pDialog.disclose();
ArrallCities=新字符串[citydata.size()];
对于(int index=0;index
只需在该数组的第一个位置添加“选择国家”,然后长度=响应项+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);
arallcities=新字符串[citydata.size()+1];
阵列城市[0]=“选择国家”//位置0处的项目
int j=1;
对于(int index=0;index
您可以编辑
// Making a request to url and getting response
citydata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(CITY_URL, ServiceHandler.GET);
//向url发出请求并获得响应
citydata=newarraylist();
字符串jsonStr=sh.makeServiceCall(CITY\uURL,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);
//向url发出请求并获得响应
citydata=newarraylist();
HashMap_选择_国家=新建HashMap();
地图选择国家。放置(“0”,“选择国家”);
城市数据。添加(地图选择国家)//在此添加新的
字符串jsonStr=sh.makeServiceCall(CITY\uURL,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);
}
ArrayList listcountry\u id,listcountry;
listcountry_id=新的ArrayList();
listcountry=新的ArrayList();
试试{
JSONArray jarr=新的JSONArray(“此处为您的响应数据”);
for(int i=0;i arg0){
//TODO自动生成的方法存根
}
});
私有void bindnerdata(){
//TODO自动生成的方法存根
ArrayAdapter喷丝头位置=新ArrayAdapter(
getApplicationContext(),R.layout.spinneritem,listcountry);
喷丝头位置.setDropDownViewResource(R.layout.spinnerpopup);
数据设置适配器(喷丝头位置);
}
//spinneritem、spinnerpopup是文本视图布局,您可以在其中设置文本颜色和颜色背景
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;
}
}
上述解决方案将显示默认的“选择城市”值,选择微调器后,它将只显示城市名称列表,而不显示默认值。在向列表添加json响应之前,请尝试这样做,HashMap defaultmap=new HashMap();defaultmap.put(“选择国家”、“选择国家”);citydata.add(defaultmap)@ajantha我可以在哪里添加
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;
}
}
adapterallcities = new HintAdapter(
Registration.this,
android.R.layout.simple_spinner_dropdown_item, arrallcitiies);