我正在尝试将值动态加载到微调器中,但是在这里出现错误“ android.R.layout.simple_spinner_item,颜色”,错误是:
错误:(109,52)错误:没有为ArrayAdapter(GuestListFragment,int,String [])找到合适的构造函数
构造函数ArrayAdapter.ArrayAdapter(Context,int,int)不适用
(参数不匹配; GuestListFragment无法转换为Context)
构造函数ArrayAdapter.ArrayAdapter(Context,int,String [])不适用
(参数不匹配; GuestListFragment无法转换为Context)
构造函数ArrayAdapter.ArrayAdapter(Context,int,List)不适用
(参数不匹配; GuestListFragment无法转换为Context)
// Spinner
colorSpinner = (Spinner) guestView.findViewById(R.id.dateSpinner);
// Array of choices
String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};
// Update Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
colorSpinner.setAdapter(spinnerArrayAdapter);
最佳答案
尝试以下代码:
String items[] = new String[2];
items[0] = String.valueOf("Item #1");
items[0] = String.valueOf("Item #2");
ArrayAdapter<String> spinnerArrayAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(spinnerArrayAdapter);
如果您在一个片段中,则将“ this”替换为“ getContext()”