Spinner 对象上的 setAdapter 方法 - 我们是否始终需要将其实现为 ArrayAdapter 的扩展?

发布于 2024-10-21 07:20:59 字数 1361 浏览 1 评论 0原文

考虑以下代码,这是使用支持其功能的数组中的数据定义微调器的简单方法:

public class HelloAndroid extends Activity implements AdapterView.OnItemSelectedListener {
    Spinner myspinner;
    ArrayAdapter<String> myadapter;
    final String[] sample_data={"Sankar", "Sunita", "Hari", "Susha", "Lovely"};

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        myspinner=(Spinner)findViewById(R.id.myspinner);
        myadapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sample_data);
        myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        myspinner.setOnItemSelectedListener(this);
        myspinner.setAdapter(myadapter);
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(this, myadapter.getItem(arg2), Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        Toast.makeText(this, "Nothing Selected", Toast.LENGTH_SHORT).show();
    }
}

我的观点是,为了实现微调器,我必须使用 ArrayAdapter。我不能将 Spinner 直接与 SpinnerAdapter 的实现关联吗? SpinnerAdapter 没有像 ArrayAdapter 那样的 setDropDownViewResource 方法,那么我们如何设置应该在微调器上显示的下拉视图资源呢?我很困惑,当 SpinnerAdapter 无法提供微调器所需的两个视图(字段和下拉列表)时,它如何充当微调器的适配器?

任何帮助表示赞赏。

Consider the following code, which is a simple way of defining a spinner with data from an Array backing its function:

public class HelloAndroid extends Activity implements AdapterView.OnItemSelectedListener {
    Spinner myspinner;
    ArrayAdapter<String> myadapter;
    final String[] sample_data={"Sankar", "Sunita", "Hari", "Susha", "Lovely"};

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        myspinner=(Spinner)findViewById(R.id.myspinner);
        myadapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sample_data);
        myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        myspinner.setOnItemSelectedListener(this);
        myspinner.setAdapter(myadapter);
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(this, myadapter.getItem(arg2), Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        Toast.makeText(this, "Nothing Selected", Toast.LENGTH_SHORT).show();
    }
}

My point here is that in order to implement the spinner, I am having to use an ArrayAdapter. Can't I associate the Spinner directly with an implementation of SpinnerAdapter? SpinnerAdapter does not have a setDropDownViewResource method like ArrayAdapter, so how would we set the drop down view resource that should show on the spinner? I'm am confused on how SpinnerAdapter is serving its purpose as an adapter for a Spinner when it cannot provide the two view (field and dropdown) that the spinner requires?

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

人心善变 2024-10-28 07:21:00

SpinnerAdapter 是一个接口,因此仅定义了支持 Spinner 所需的一组方法。

ArrayAdapter 是一个类,并实现 SpinnerAdapter 接口方法以及其他功能以实际提供工作实现。

您可以编写自己的类来实现 SpinnerAdapter 接口中的方法,以显示您想要在下拉列表中显示的内容,但由于 SpinnerAdapter 不包含任何代码,因此您无法使用 SpinnerAdapter 本身。

最好的思考方式是 SpinnerAdapter 告诉您需要做什么(因为它是一个接口),而 ArrayAdapter 实际上会执行它(因为它是一个类)。

SpinnerAdapter is an interface and so only defines a set of methods which are required to support a Spinner.

ArrayAdapter is a class, and implements the SpinnerAdapter interface methods along with other functionality to actually provide a working implementation.

You can write your own class which implements the methods from the SpinnerAdapter interface to show what you want to show in a drop down, but as SpinnerAdapter contains no code, you can't use the SpinnerAdapter itself.

The best way to think of it is SpinnerAdapter tells you what you need to do (as it is an interface), and ArrayAdapter actually does it (because it is a class).

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