如何延长微调,以实现新的功能? [英] How can I extend spinner to implement a new function?
问题描述
我想在我的code扩展微调的Android类来实现一个新的功能,但由于某种原因,这并不工作。
这是扩展的类:
进口android.content.Context;
进口android.widget.AdapterView;
进口android.widget.Spinner;公共类CustomSpinner扩展微调{ 公共CustomSpinner(上下文的背景下){
超级(上下文);
} 公共无效setSelectionByItemId(适配器视图<>父,长ID){
的for(int i = 0; I< parent.getCount();我++){
长itemIdAtPosition = parent.getItemIdAtPosition(I)
如果(itemIdAtPosition == ID){
parent.setSelection(ⅰ);
打破;
}
}
}}
这是我在这个实例化类的方式:
CustomSpinner微调=(CustomSpinner)findViewById(R.id.sphofentries);
这给我一个运行时错误。
这一切都是如果R.id.sphofentries在我的布局作为一个微调声明。
但现在,如果我宣布sphofentries作为一个CustomSpinner我得到一个运行时错误只是在那一刻我的布局设置为活动:
的setContentView(R.layout.settings);
另外,我pretty肯定,问题是,我需要声明sphofentries作为CustomSpinner,因为如果我这样做:
CustomSpinner微调=新CustomSpinner(本);
微调=(CustomSpinner)findViewById(R.id.sphofentries);
这不用问题低谷的第一线,但在第二次则问题不是创建一个新的CustomSpinner但在这CustomSpinner设置sphofentries(这与sphofentries宣布像微调不是CustomSpinner)给出了一个运行时错误
也许我做错事的布局,这是我声明sphofentries作为CustomSpinner方式:
< CustomSpinner
机器人:ID =@ + ID / sphofentries
机器人:layout_below =@ + ID / tvhofentries
机器人:layout_width =300dip
机器人:layout_height =WRAP_CONTENT
机器人:重力=CENTER_HORIZONTAL
/>
最后还有两个原因不能正常工作,这两个previous的答案是正确的:
-
这是必要的也是定义与AttributeSet中的参数第二个构造函数。
公共CustomSpinner(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS); -
在微调必须定义为一个CustomSpinner,需要所有的完全限定名声明的布局:
< net.domain.package.CustomSpinner
机器人:ID =@ + ID / sphofentries
机器人:layout_below =@ + ID / tvhofentries
机器人:layout_width =300dip
机器人:layout_height =WRAP_CONTENT
机器人:重力=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"
/>
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.
public CustomSpinner(Context context, AttributeSet attrs) { super(context, attrs);
In the layout the Spinner must be defined as a CustomSpinner and needs to be declared with all the fully qualified name:
<net.domain.package.CustomSpinner android:id="@+id/sphofentries" android:layout_below="@+id/tvhofentries" android:layout_width="300dip" android:layout_height="wrap_content" android:gravity="center_horizontal" />
这篇关于如何延长微调,以实现新的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!