每当我的微调器选择一个新项目时,我想调用一个动作。当前设置是按下按钮时调用操作。
我的方法:
创建一个新功能:public void onSpinnerItemSelection(View V){...}
然后在content_home.xml
我将onSpinnerItemSelection
添加到onClick
Spinner
。
这不起作用。我不熟悉Android错误,而且我很难解释这些错误。
Unfortunately, myApp has stopped
Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
答案 0 :(得分:1)
一步一步:
1ªDeclare变量来自布局Spinner sp;
这个案例中的arraylist ArrayList listTeams;
和适配器Adapter adapter;
2ªinstances
private void instances() {
sp = (Spinner) findViewById(R.id.spinner);
listaTeam = new ArrayList();
3º为适配器添加项目
private void listAdapter() {
listaTeam.add(new Equipo(R.string.Atleti));
adaptador = new Adaptador(MainActivity.this, listaEquipo);
sp.setAdapter(adapter);
现在我们采取行动,使用onItemSelected。
public void onItemSelected(AdapterView parent,View view,int position,long id){
Toast.makeText("this is my team", Toast.LENGTH_LONG).show();
}
答案 1 :(得分:0)
以下是一个例子:
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText("log", "position = " position, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText("log", "onNothingSelected", Toast.LENGTH_LONG).show();
}
});