有人可以帮助我。我刚刚开始Android开发,我想创建一个具有下拉菜单的应用程序。我的代码中似乎有两个错误,我无法解决。错误发生在实现OnItemSelectedListner
和spinner.setOnItemSelectedListener(this);
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
int Cups = 1;
int Price = 1;
int Sum = 0;
private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3", "item 4", "item 5"};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position)
{
case 0:
Price=0.5;
break;
case 1:
Price=1;
break;
case 2:
Price=2;
break;
case 3:
Price=3;
break;
case 4:
Price=4;
break;
}
}
答案 0 :(得分:0)
第一个错误是实现正确的OnItemClickListener
implements AdapterView.OnItemClickListener
second 是从侦听器覆盖正确的方法
@Override
onItemClick(AdapterView<?> parent, View view, int position, long id) {
}