这是我的主要活动
put (Integer,capture<? extends com.cae_analysis.model.data.nx.NXPersistentDataInt>) in HashMap cannot be applied to (Integer,capture<? extends com.cae_analysis.model.data.nx.NXPersistentDataInt>)
这里我想为spinner设置onItemClickListener。我试过创建一个Inner类并创建一个匿名类,我甚至尝试过实现AdapterView.onItemSelectedListener并传递
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
}
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
}
@Override
public void onButtonClicked(int buttonCode) {
}
}
但是android studio的每一种方式都给了我编译时的错误。 什么是可能和正确的方法来setOnItemSelected旋转器和如何?
错误: enter image description here
代码:
spinner.setOnItemSelectedListener(this);
使用匿名类:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener, AdapterView.OnItemSelectedListener{
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(this);
}
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
}
@Override
public void onButtonClicked(int buttonCode) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
使用内部类:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
}
@Override
public void onButtonClicked(int buttonCode) {
}
}
这种方式没有错误,但是它是正确的:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener{
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(new spinner_class());
}
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
}
@Override
public void onButtonClicked(int buttonCode) {
}
public class spinner_class implements AdapterView.OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
答案 0 :(得分:1)
在oncreate方法中添加setOnItemClickListener
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG,"@@@setOnItemClickListener position:"+position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
}
@Override
public void onButtonClicked(int buttonCode) {
}
}
答案 1 :(得分:0)
我敢打赌(因为我们不知道代码中的错误消息和确切方法setOnItemSelectedListener
调用位置),您必须实现OnItemSelectedListener
,不 AdapterView.OnItemSelectedListener
接口在MainActivity
。描述here。