如何扩展 spinner 来实现新功能?

发布于 2024-10-08 19:36:44 字数 1830 浏览 5 评论 0原文

我试图在我的代码中扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怂人 2024-10-15 19:36:44

最后有两个原因导致此功能无法正常工作,前面的两个答案是正确的:

  1. 还需要使用 AttributeSet 参数定义第二个构造函数。

    public CustomSpinner(Context context, AttributeSet attrs) {
        超级(上下文,属性);
    
  2. 在布局中,Spinner 必须定义为 CustomSpinner,并且需要使用所有完全限定名称进行声明:

    >
    

Finally there was two reasons for this to not work properly, the two previous answers are right:

  1. It's necessary to define also the second constructor with the AttributeSet parameter.

    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    
  2. 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"
    />
    
時窥 2024-10-15 19:36:44

这是一个常见的错误。只需添加这个构造函数,它是布局充气器调用的构造函数:

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

It's a common mistake. Just add this constructor, which is the one called by the layout inflater:

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}
玻璃人 2024-10-15 19:36:44

sphofentries 是 Spinner 还是 CustomSpinner

需要在布局中将其声明为 CustomSpinner,以便您在代码中将其转换为 CustomSpinner

反之亦然。您可以将 CustomSpinner 转换为 Spinner,因为它是一个子类。

Is sphofentries a Spinner or a CustomSpinner?

It would need to be declared as a CustomSpinner in your layout for you to cast it as a CustomSpinner in your code.

The other way around would work. You would be able to cast a CustomSpinner as a Spinner, because it is a subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文