我想设置一个微调器的位置。我有一个适配器的字符串数组,即
final String[] cat = { "Highest", "Lowest", "Most Recent"};
但我希望我的微调器最初显示一个空白。所以我尝试了这个。
mSpinner.setSelection(-1);
但这并不能解决我的问题。任何想法如何做到这一点?非常需要和赞赏帮助。感谢。
更新 我的代码:
private void displayDialog() {
// TODO displayDialog
final ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sortBy);
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.dialog_layout, null);
promptsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
final Spinner mSpinner= (Spinner) promptsView
.findViewById(R.id.spDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Sort By...");
builder.setIcon(R.drawable.launcher);
mSpinner.setAdapter(adp);
mSpinner.setSelection(-1);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
strSpinner = mSpinner.getSelectedItem().toString();
if(strSpinner.equals("Highest Price")){
highest.setTypeface(Typeface.DEFAULT_BOLD);
lowest.setTypeface(Typeface.DEFAULT);
location.setTypeface(Typeface.DEFAULT);
price = dbHelper.sortHighestPrice();
adapter = new MyCustomAdapter(imgs, text, price);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else if (strSpinner.equals("Lowest Price")){
highest.setTypeface(Typeface.DEFAULT);
lowest.setTypeface(Typeface.DEFAULT_BOLD);
location.setTypeface(Typeface.DEFAULT);
price = dbHelper.sortLowestPrice();
adapter = new MyCustomAdapter(imgs, text, price);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else if (strSpinner.equals("Location")) {
highest.setTypeface(Typeface.DEFAULT);
lowest.setTypeface(Typeface.DEFAULT);
location.setTypeface(Typeface.DEFAULT_BOLD);
} else {
Log.d("Default", "Default");
}
}
答案 0 :(得分:1)
让你的第一个项目为空白。
final String[] cat = {"", "Highest", "Lowest", "Most Recent"};
答案 1 :(得分:0)
将第一个项目设为空白,默认情况下设置微调器的选择为0。
答案 2 :(得分:0)
将Spinner默认值位置设置为-1。
请点击链接了解详情:
How to make an Android Spinner with initial text "Select One"