[英]Cannot get spinner to work in Android Studio
I am having great difficulty understanding how to code a spinner. 我很难理解如何编写微调器。 I've looked at the many items on this site and others but don't understand why it has to be SO complicated, with some solutions even involving creating a class.
我已经查看了该站点上的许多项目以及其他项目,但不明白为什么它必须如此复杂,有些解决方案甚至涉及创建类。 All I want to be able to do is, when the user selects one of the items from the spinner drop-down, which one has been clicked needs to be identified then the code triggers a new activity or a method or some other action.
我想要做的就是,当用户从微调器下拉列表中选择一项时,需要确定单击了哪些项,然后代码触发新的活动或方法或其他某种动作。 The web site I-Programmer seemed to offer a relatively simple solution and I have followed the coding instructions to the letter but it doesn't work.
网站I-Programmer似乎提供了一个相对简单的解决方案,并且我已按照字母上的编码说明进行操作,但它不起作用。 I have written to them but they haven't replied yet.
我已经写信给他们,但他们还没有答复。 Their code now follows, but I don't understand how it can work, as there doesn't seem to be a link between the AdapterView block and the actual spinner stuff before it.
现在,他们的代码如下,但是我不知道它如何工作,因为在AdapterView块和它之前的实际微调器部件之间似乎没有链接。 It doesn't compile as is.
它不会按原样编译。 When I add a few semi-colons and braces, it DOES compile but still doesn't work.
当我添加一些分号和花括号时,它可以编译,但仍然无法正常工作。 Can anyone sort it out for me please?
谁能帮我解决这个问题?
package com.example.owner.spinners;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] country = {"Canada", "Mexico", "USA"};
ArrayAdapter<String> stringArrayAdapter=
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
country);
Spinner spinner=
(Spinner)findViewById(R.id.spinner);
spinner.setAdapter(stringArrayAdapter);
}
AdapterView.OnItemSelectedListener onSpinner=
new AdapterView.OnItemSelectedListener(){ // Error 1 here
AdapterView.OnItemSelectedListener onSpinner =
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(
AdapterView<?> parent,
View view,
int position,
long id) { // Does my 'if' code go here?
}
@Override
public void onNothingSelected(
AdapterView<?> parent) { // and here?
}
} // Error 2 here. ; expected
} // Error 3 here. ; expected
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
...
}
Error 1 reads, Class 'Anonymous class derived from OnItemSelectedListener' must either be declared abstract or implement abstract method 'onNothingSelected (AdapterView)' in 'OnItemSelectedListener' I'm afraid I don't follow all that. 错误1读取,类'从OnItemSelectedListener派生的匿名类'必须声明为抽象,或在'OnItemSelectedListener'中实现抽象方法'onNothingSelected(AdapterView)',恐怕我不遵循所有这些方法。 I have really tried.
我真的试过了 Thanks in advance.
提前致谢。
If you don't want to use an extra class, you can use an anonymous inner class . 如果您不想使用额外的类,则可以使用匿名内部类 。
Be sure to register the Listener to your spinner. 确保将监听器注册到微调器。
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] country = {"Canada", "Mexico", "USA"};
ArrayAdapter<String> stringArrayAdapter= new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, country);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setAdapter(stringArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() // register the listener
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// User selected item
Toast.makeText(getApplicationContext(), country[position] + " selected!", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
First, why you have a compile error: 首先,为什么会有编译错误:
Error 1 - because you tried create a variable in this place, but you can't do that. 错误1-因为您尝试在此位置创建变量,但是您不能这样做。 In this place you can use a existing variable/field or create a simple anonymous class.
在这里,您可以使用现有的变量/字段或创建简单的匿名类。
Error 2 - compiler expected ;
错误2-预期的编译器
;
because you are ended of the part of code, so you have to end this by ;
因为您已经结束了代码的一部分,所以您必须以
;
结尾;
. 。
Error 3 - it the same situation like in Error 2. 错误3-与错误2中的情况相同。
So, I a little improved your code and added some comments. 因此,我对您的代码做了一些改进,并添加了一些注释。 I hope now the code is more understandable for you.
我希望现在该代码对您来说更容易理解。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create a adapter
String[] country = {"Canada", "Mexico", "USA"};
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, country);
// create a spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// add adapter to spinner
spinner.setAdapter(stringArrayAdapter);
// create listener and add to spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// put code which recognize a selected element
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.