动态加载微调器值

时间:2016-10-14 21:54:04

标签: java android mobile

我正在尝试动态地将值加载到我的微调器中,但我在这里收到错误“this,android.R.layout.simple_spinner_item,colors”错误是:

错误:(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);

3 个答案:

答案 0 :(得分:1)

请尝试以下代码:

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);
  

如果你在一个片段中取代&#34;这个&#34; by&#34; getContext()&#39;

答案 1 :(得分:0)

您的Context参数存在问题。同时尝试更改new ArrayAdapter<String> - &gt; new ArrayAdapter<>

ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout. simple_spinner_item, colors);

答案 2 :(得分:0)

使用 ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_spinner_item,colors);

或 ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,colors);

而不是 ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_spinner_item,colors);