我在我的应用中实现了一个微调器。我正在显示学校列表,但我想要一个默认值/提示,以便用户知道选择一所学校。我也希望这样做而不必将提示作为列表项。有什么想法吗?
到目前为止,这是我的微调代码:
selectSchoolSpinner = (Spinner) findViewById(R.id.select_school_spinner);
spinnerSchoolList = new ArrayList<String>();
spinnerSchoolList.add("Please select School");
//I don't want to have a prompt like the latter
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerSchoolList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectSchoolSpinner.setAdapter(spinnerAdapter);
答案 0 :(得分:19)
您应该在其构造函数中为所需文本创建带有String的自定义ArrayAdapter。然后,您应该使用默认文本切换数组的第一个元素,然后在用户单击微调器后,它应切换回原始数组。
示例:
活动类:
public class SpinnerActivity extends Activity {
Spinner spinner;
String defaultTextForSpinner = "Your deafult text here";
String[] arrayForSpinner = {"One", "Two", "Three"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = (Spinner) findViewById(R.id.your_spinner);
spinner.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, arrayForSpinner, defaultTextForSpinner));
}
}
CustomSpinnerAdapter类:
public class CustomSpinnerAdapter extends ArrayAdapter<String>{
Context context;
String[] objects;
String firstElement;
boolean isFirstTime;
public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
super(context, textViewResourceId, objects);
this.context = context;
this.objects = objects;
this.isFirstTime = true;
setDefaultText(defaultText);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(isFirstTime) {
objects[0] = firstElement;
isFirstTime = false;
}
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = objects[0];
objects[0] = defaultText;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinner_text);
label.setText(objects[position]);
return row;
}
}
答案 1 :(得分:12)
尝试使用setSelection方法设置默认文本:
spinner.setSelection(adapter.getPosition(DEFAULT_CURRENCY_TYPE))
看看这个链接:How to make an Android Spinner with initial text "Select One"。它非常有用(@aaronvargas答案是你需要的)。
答案 2 :(得分:1)
@Override
protected void onPostExecute(PositionBean[] result) {
String[] spinnerArray = new String[result.length];
if(result!=null) {
for (int i = 0; i < result.length; i++) {
String positionName = result[i].getPositionName();
spinnerArray[i] = result[i].getPositionName();
}
ArrayList<String> positionList = new ArrayList<String>(Arrays.asList(spinnerArray));
positionList.add(0,"select");
String [] positions = positionList.toArray(new String[positionList.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineActivity.this, android.R.layout.simple_spinner_item,
positions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
positionSpinner.setAdapter(adapter);
}else{
Log.d("NULL", "This is null");
}
}