向spinner android添加提示
我正在从web服务添加微调器值。我想在edittext中添加一个提示,比如“选择你的国家”。我尝试使用自定义适配器,但没有成功。有什么好办法吗向spinner android添加提示,android,android-spinner,Android,Android Spinner,我正在从web服务添加微调器值。我想在edittext中添加一个提示,比如“选择你的国家”。我尝试使用自定义适配器,但没有成功。有什么好办法吗 public class MainActivity extends AppCompatActivity { private EditText card_number_et, cvv_et, card_holder_name_et, multiline_et, postcode_et; private Spinner country_sp
public class MainActivity extends AppCompatActivity {
private EditText card_number_et, cvv_et, card_holder_name_et, multiline_et, postcode_et;
private Spinner country_spin, state_spin, city_spin;
private Button pay_btn;
static EditText expiry_date_et;
private ArrayAdapter adapter, adapter1, adapter2, adapter3;
private ArrayList<String> namelist = new ArrayList<>();
private ArrayList<String> idlist = new ArrayList<>();
private HashMap<String,String> spinnerMap1, spinnerMap2, spinnerMap3;
private String countryid, stateid, cityid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
country_spin = (Spinner)findViewById(R.id.country_spin);
state_spin = (Spinner)findViewById(R.id.state_spin);
city_spin = (Spinner)findViewById(R.id.city_spin);
card_number_et = (EditText)findViewById(R.id.card_number_et);
expiry_date_et = (EditText)findViewById(R.id.expiry_date_et);
cvv_et = (EditText)findViewById(R.id.cvv_et);
card_holder_name_et = (EditText)findViewById(R.id.card_holder_name_et);
multiline_et = (EditText)findViewById(R.id.multiline_et);
postcode_et = (EditText)findViewById(R.id.postcode_et);
pay_btn = (Button)findViewById(R.id.pay_btn);
expiry_date_et.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
DatePickerFragment date = new DatePickerFragment();
date.show(getFragmentManager(), "Date");
}
});
country_spin.setPrompt("Select your country");
state_spin.setPrompt("Select your state");
city_spin.setPrompt("Select your city");
getCountryList();
country_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String countryname = country_spin.getSelectedItem().toString();
System.out.println("somucountryname"+" "+countryname);
countryid = spinnerMap1.get(countryname);
System.out.println("somucountrynameid"+" "+countryid);
if(countryid != null && !countryid.trim().isEmpty()){
getStateList(countryid);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
state_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String statename = state_spin.getSelectedItem().toString();
System.out.println("somustatename"+" "+statename);
stateid = spinnerMap2.get(statename);
System.out.println("somustateid"+" "+stateid);
if(stateid != null && !stateid.trim().isEmpty()){
getCityList(stateid);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
pay_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makepayment();
}
});
} // onCreate ends
private void getCountryList() {
if(namelist!=null )namelist.clear();
if(idlist!=null )idlist.clear();
StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.example.com/webservice/allreligionlisting",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
JSONArray jArray = jObj.getJSONArray("all_country");
for(int i=0;i<jArray.length();i++){
JSONObject main = jArray.getJSONObject(i);
namelist.add(main.getString("name"));
idlist.add(main.getString("code"));
String[] spinnerArray = new String[idlist.size()];
spinnerMap1 = new HashMap<>();
for (int j = 0; j < idlist.size(); j++)
{
spinnerMap1.put(namelist.get(j),idlist.get(j));
spinnerArray[j] = namelist.get(j);
}
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(R.layout.spinnerlayout);
country_spin.setAdapter(adapter);
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
/*@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}*/
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
private void getStateList(final String countryId) {
if(namelist!=null )namelist.clear();
if(idlist!=null )idlist.clear();
StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.example.com/webservice/allstate/"+countryId,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
if(jObj.getString("ACK").equals("1")){
JSONArray jArray = jObj.getJSONArray("all_state");
for(int i=0;i<jArray.length();i++){
JSONObject main = jArray.getJSONObject(i);
namelist.add(main.getString("name"));
idlist.add(main.getString("id"));
String[] spinnerArray = new String[idlist.size()];
spinnerMap2 = new HashMap<>();
for (int j = 0; j < idlist.size(); j++)
{
spinnerMap2.put(namelist.get(j),idlist.get(j));
spinnerArray[j] = namelist.get(j);
}
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(R.layout.spinnerlayout);
state_spin.setAdapter(adapter);
}
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
/*@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}*/
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
private void getCityList(final String stateId) {
if(namelist!=null )namelist.clear();
if(idlist!=null )idlist.clear();
StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.example.com/webservice/allcity/"+stateId,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
if(jObj.getString("ACK").equals("1")){
JSONArray jArray = jObj.getJSONArray("all_city");
for(int i=0;i<jArray.length();i++){
JSONObject main = jArray.getJSONObject(i);
namelist.add(main.getString("name"));
idlist.add(main.getString("id"));
String[] spinnerArray = new String[idlist.size()];
spinnerMap3 = new HashMap<>();
for (int j = 0; j < idlist.size(); j++)
{
spinnerMap3.put(namelist.get(j),idlist.get(j));
spinnerArray[j] = namelist.get(j);
}
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(R.layout.spinnerlayout);
city_spin.setAdapter(adapter);
}
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
/*@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}*/
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
public类MainActivity扩展了AppCompatActivity{
私人编辑文本卡号、cvv、持卡人姓名、多行、邮政编码等;
私人纺纱机国家纺纱,国家纺纱,城市纺纱;
私人按钮支付;
静态编辑文本到期日等;
专用阵列适配器,适配器1,适配器2,适配器3;
private ArrayList namelist=new ArrayList();
private ArrayList idlist=new ArrayList();
私有HashMap spinnerMap1、spinnerMap2、spinnerMap3;
私有字符串countryid、stateid、cityid;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
country_spin=(微调器)findViewById(R.id.country_spin);
state\u spin=(微调器)findviewbyd(R.id.state\u spin);
城市旋转=(旋转器)findViewById(R.id.city\u旋转);
card_number_et=(EditText)findViewById(R.id.card_number_et);
到期日=findViewById(R.id.expiration\u date\u et);
cvv_et=(EditText)findViewById(R.id.cvv_et);
卡片持有人姓名et=(EditText)findViewById(R.id.card持有人姓名et);
multiline_et=(EditText)findViewById(R.id.multiline_et);
postcode_et=(EditText)findViewById(R.id.postcode_et);
pay_btn=(按钮)findviewbyd(R.id.pay_btn);
expiration\u date\u et.setOnClickListener(新视图.OnClickListener(){
@TargetApi(Build.VERSION\u code.LOLLIPOP)
@凌驾
公共void onClick(视图v){
DatePickerFragment date=新的DatePickerFragment();
显示(getFragmentManager(),“日期”);
}
});
country_spin.setPrompt(“选择您的国家”);
state_spin.setPrompt(“选择您的状态”);
city_spin.setPrompt(“选择您的城市”);
getCountryList();
country_spin.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
字符串countryname=country\u spin.getSelectedItem().toString();
System.out.println(“somucountryname”+“”+countryname);
countryid=spinnerMap1.get(countryname);
System.out.println(“somucountrynameid”+“”+countryid);
if(countryid!=null&&!countryid.trim().isEmpty()){
getStateList(countryid);
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
state_spin.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
字符串statename=state_spin.getSelectedItem().toString();
System.out.println(“soCustomateName”+“”+statename);
stateid=spinnerMap2.get(statename);
System.out.println(“somustateid”+“”+stateid);
if(stateid!=null&&!stateid.trim().isEmpty()){
getCityList(stateid);
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
pay_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
付款();
}
});
}//onCreate结束
私有void getCountryList(){
如果(namelist!=null)namelist.clear();
if(idlist!=null)idlist.clear();
StringRequest StringRequest=新的StringRequest(Request.Method.GET)http://www.example.com/webservice/allreligionlisting",
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jObj=新的JSONObject(响应);
JSONArray jArray=jObj.getJSONArray(“所有国家”);
对于(int i=0;i我最近在我的项目中实现了这一点。您只需在填充arraylist
之前,在arraylist的0位置传递“选择项”,然后用数据填充它。之后,在微调器适配器中覆盖以下方法-
@Override
public boolean isEnabled(int position){
if(position == 0)
{
// Disable the first item "Select Item" from Spinner
// First item will be use for hint
return false;
}
else
{
return true;
}
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if(position == 0){
// Set the hint "Select Item" text color gray
tv.setTextColor(Color.GRAY);
}
else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
您可以看到示例。我最近在我的项目中实现了这一点。您只需在填充arraylist
之前,在arraylist的0位置传递“Select Item”,然后用数据填充它。然后在微调器适配器中覆盖以下方法-
@Override
public boolean isEnabled(int position){
if(position == 0)
{
// Disable the first item "Select Item" from Spinner
// First item will be use for hint
return false;
}
else
{
return true;
}
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if(position == 0){
// Set the hint "Select Item" text color gray
tv.setTextColor(Color.GRAY);
}
else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
您可以看到示例。see或我在自定义适配器中使用了第一个链接的代码,但应用程序崩溃了。您可以进行如下操作:将列表中的第一个元素设为提示并将其余元素添加到列表中,最后在onItemSelected()中忽略对第一个元素的选择我检查了NoDefaultSpinner
的代码,但它不工作请参见,或者我在自定义适配器中使用了第一个链接的代码,但应用程序崩溃了。你可以做一个类似的黑客操作,将列表中的第一个元素作为提示并将其余元素添加到列表中,最后在onItemSelected()中忽略第一个元素的选择我检查了NoDefaultSpinner
的代码,但它不工作