我需要将默认值文本“选择你的设备”值设置为我的微调器下拉列表,我把它作为列表项,因此它充当下拉列表项和我隐藏下拉列表,因为我的下拉列表中有空格。
我该怎么做?
这是我用过的代码
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
/*Setup a Spinner*/
mToolbarSpinner = (Spinner) findViewById(R.id.spinner_toolbar);
/*Manage the spinner*/
mToolbarSpinnerNavigationItems = new ArrayList<SpinnerNavItem>();
mToolbarSpinnerNavigationItems.add(new SpinnerNavItem("Select your Seebo", R.drawable.deviceonline));
/*setting the mToolbarSpinnerNavigationItems to list_item_title_navigation.xml*/
mArrayAdapter = new ArrayAdapter(this, R.layout.list_item_title_navigation,mToolbarSpinnerNavigationItems){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// remember to inflate custom layout for spinner item
// then set anything you want here
// remember to use convertView.findViewById(), not just findViewById()
// this is an example
// inflate layout
convertView = View.inflate(MainActivity.this, R.layout.list_item_title_navigation, null);
// get view
TextView tvText1 = (TextView) convertView.findViewById(R.id.devicetitle);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.deviceicon);
imgIcon.setVisibility(View.GONE);
//imgIcon.setImageResource(mToolbarSpinnerNavigationItems.get(position).getIcon());
// set content
tvText1.setText(mToolbarSpinnerNavigationItems.get(position).getTitle());
// return
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return convertView;
}
};
//setting adapter to Spinner
mToolbarSpinner.setAdapter(mArrayAdapter);
答案 0 :(得分:8)
就我对您的问题的理解而言,您可以做的是 通过启动来覆盖Spinner
view
它的索引从-1而不是0 ,这样就可以使数组的第一个条目成为你想要的数组。
选择您的设备
StackOverflow上已存在多个问题,您可以参考并进行覆盖,建议使用通用解决方案。
参考here。
或者反过来说,你也可以使用这段代码set the customized default position of spinner:
ArrayAdapter<String> spinnerAdap = (ArrayAdapter<String>) mySpinner.getAdapter();
int spinnerPosition = messgAdap.getPosition(theDefaultValue);
mySpinner.setSelection(spinnerPosition);
答案 1 :(得分:3)
从下拉列表中选择项目: 在设置适配器
之后{{1}}
答案 2 :(得分:2)
你可以使用喜欢,
final ArrayList<String> strDeviceArr = new ArrayList<>();
strDeviceArr.add("Select device");
strDeviceArr.add("Nexus X");
strDeviceArr.add("Nexus XL");
spnrDevice.setAdapter(new ArrayAdapter<>(MainActivity.this, R.layout
.layout_spinner_textview, R.id.textView, strDeviceArr));
spnrDevice.setSelection(0); //set default selection to 0
spnrDevice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String valueSelected = strDeviceArr.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 3 :(得分:0)
使用以下顺序:
dropdown = (Spinner) findViewById(R.id.target_dropdown);
List<String> targetList = new ArrayList<>(targetSet);
targetList.add(0, "Select a device");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, targetList);
dropdown.setAdapter(adapter);
dropdown.setSelection(0);
答案 4 :(得分:0)
请在下面找到代码
val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, gender_array)
genderSpinner!!.adapter = arrayAdapter as SpinnerAdapter?
**genderSpinner!!.setSelection(2)**
genderSpinner!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
select_gender = parent?.getItemAtPosition(position).toString()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}