如何扩展 spinner 来实现新功能?
我试图在我的代码中扩展 Spinner
Android 类来实现新功能,但由于某种原因这不起作用。
这是扩展类:
import android.content.Context;
import android.widget.AdapterView;
import android.widget.Spinner;
public class CustomSpinner extends Spinner {
public CustomSpinner(Context context) {
super(context);
}
public void setSelectionByItemId(AdapterView<?> parent, long id){
for (int i = 0; i < parent.getCount(); i++) {
long itemIdAtPosition = parent.getItemIdAtPosition(i);
if (itemIdAtPosition == id) {
parent.setSelection(i);
break;
}
}
}
}
这就是我实例化此类的方式:
CustomSpinner spinner = (CustomSpinner) findViewById(R.id.sphofentries);
这在运行时给我一个错误。
所有这一切都是在我的布局中将 R.id.sphofentries 声明为微调器的情况下实现的。
但是现在,如果我将 sphofentries
声明为 CustomSpinner
,我会在将布局设置为 Activity 时收到运行时错误:
setContentView(R.layout.settings);
而且我很确定问题是我需要将 sphofentries
声明为 CustomSpinner
因为如果我这样做:
CustomSpinner spinner = new CustomSpinner(this);
spinner = (CustomSpinner) findViewById(R.id.sphofentries);
这在第一行中没有问题,但在第二行中给出运行时错误,那么问题就不是' t 创建一个新的 CustomSpinner
但在此 CustomSpinner
中设置 sphofentries
(这与 sphofentries
声明类似 Spinner
不是 CustomSpinner
)。
也许我在布局中做错了什么,这就是我将 sphofentries
声明为 CustomSpinner
的方式:
<CustomSpinner
android:id="@+id/sphofentries"
android:layout_below="@+id/tvhofentries"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
I'm trying to extend Spinner
Android class in my code to implement a new function, but for some reason this didn't work.
This is the extended class:
import android.content.Context;
import android.widget.AdapterView;
import android.widget.Spinner;
public class CustomSpinner extends Spinner {
public CustomSpinner(Context context) {
super(context);
}
public void setSelectionByItemId(AdapterView<?> parent, long id){
for (int i = 0; i < parent.getCount(); i++) {
long itemIdAtPosition = parent.getItemIdAtPosition(i);
if (itemIdAtPosition == id) {
parent.setSelection(i);
break;
}
}
}
}
And this is the way I'm instantiating this class:
CustomSpinner spinner = (CustomSpinner) findViewById(R.id.sphofentries);
This give me an error at runtime.
All this is if R.id.sphofentries
is declared in my layout as an spinner.
But now, if I declare sphofentries
as a CustomSpinner
I get a runtime error just in the moment I set the Layout to the Activity:
setContentView(R.layout.settings);
Also I am pretty sure that the problem is that I need to declare sphofentries
as a CustomSpinner
because if I do this:
CustomSpinner spinner = new CustomSpinner(this);
spinner = (CustomSpinner) findViewById(R.id.sphofentries);
This goes without problem trough the first line but gives a runtime error in the second then the problem isn't creating a new CustomSpinner
but setting the sphofentries
in this CustomSpinner
(This with sphofentries
declared like a Spinner
not a CustomSpinner
).
Maybe I am doing something wrong in the layout, this is the way I am declaring sphofentries
as a CustomSpinner
:
<CustomSpinner
android:id="@+id/sphofentries"
android:layout_below="@+id/tvhofentries"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后有两个原因导致此功能无法正常工作,前面的两个答案是正确的:
还需要使用 AttributeSet 参数定义第二个构造函数。
在布局中,Spinner 必须定义为 CustomSpinner,并且需要使用所有完全限定名称进行声明:
Finally there was two reasons for this to not work properly, the two previous answers are right:
It's necessary to define also the second constructor with the AttributeSet parameter.
In the layout the Spinner must be defined as a CustomSpinner and needs to be declared with all the fully qualified name:
这是一个常见的错误。只需添加这个构造函数,它是布局充气器调用的构造函数:
It's a common mistake. Just add this constructor, which is the one called by the layout inflater:
sphofentries 是
Spinner
还是CustomSpinner
?需要在布局中将其声明为
CustomSpinner
,以便您在代码中将其转换为CustomSpinner
。反之亦然。您可以将
CustomSpinner
转换为Spinner
,因为它是一个子类。Is sphofentries a
Spinner
or aCustomSpinner
?It would need to be declared as a
CustomSpinner
in your layout for you to cast it as aCustomSpinner
in your code.The other way around would work. You would be able to cast a
CustomSpinner
as aSpinner
, because it is a subclass.