简体   繁体   English

Android:如何为Spinner设置DropDown View Resource?

[英]Android : how to set DropDown View Resource for Spinner?

I wrote a code for Spinner to bind array of USA States with Spinner in Android. 我为Spinner编写了一个代码,用于在Android中使用Spinner绑定美国各州的数组。 But the problem is it shows reference type data in Spinner item, see the pic 但问题是它在Spinner项目中显示引用类型数据,请参见图片

I add android.R.layout.simple_spinner_dropdown_item but not know that what to add in layout. 我添加android.R.layout.simple_spinner_dropdown_item但不知道在布局中添加什么。 I checked many exemples on google and they add simple_spinner_dropdown_item but i could not find that what to add in layout. 我检查了谷歌上的许多例子,他们添加了simple_spinner_dropdown_item,但我找不到在布局中添加的内容。 below is output and code. 下面是输出和代码。 I want to show states in list instead of this junky data. 我想在列表中显示状态而不是这个垃圾数据。

在此输入图像描述

    Spinner spStates = new Spinner(this);
        spStates.setLayoutParams(new LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT));

        final USAStates states[] = new USAStates[51];

        states[0] = new USAStates("Alabama", "AL");
        states[1] = new USAStates("Alaska", "AK");
        states[2] = new USAStates("Arizona", "AZ");
ArrayAdapter<USAStates> adapter = new ArrayAdapter<USAStates>(this, android.R.layout.simple_spinner_item, states);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spStates.setAdapter(adapter);
        spStates.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                USAStates d = states[position];
                Toast.makeText(getApplicationContext(), d.getStateAbrivation(), Toast.LENGTH_LONG).show();

            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });




public class USAStates {
        private String _Statename;
        private String _StateAbrivation;

        public USAStates(String pStatename, String pStateAbrivation) {
            Statename(pStatename);
            StateAbrivation(pStateAbrivation);
        }
        public void Statename(String pStatename) {
            _Statename = pStatename;
        }
        public void StateAbrivation(String pStateAbrivation) {
            _StateAbrivation = pStateAbrivation;
        }

        public String getStatename() {
            return _Statename;
        }
        public String getStateAbrivation() {
            return _StateAbrivation;
        }
    }

Not sure, just doing this off the top of my head but, in your USAState class override your toString method.As is it maybe that the adapter is using the default toString() hence your weird text displaying (which I presume is the class name of the USAStates class) 不确定,只是在我的脑海中做这个,但是,在你的USAState类中覆盖你的toString方法。因为它可能是适配器使用默认的toString()因此你的怪异文本显示(我认为是类名USAStates类)

for example 例如

@Override
public String toString(){
return _Statename
}

I originally accepted the toString() answer but have since found that this doesn't appear to be right. 我最初接受了toString()答案,但后来发现这似乎不对。

I had an ActionBar with spinner/drop down list and my adapter items were rendering with String.toString() value instead of of the title I set in the custom adapter. 我有一个带有微调/下拉列表的ActionBar,我的适配器项目使用String.toString()值而不是我在自定义适配器中设置的标题。 Adding toString() did fix initially until I tried to set a compound drawable in the same layout. 添加toString()最初修复,直到我尝试在同一布局中设置复合drawable。

I needed to override getDropDownView as well as getView in my adapter. 我需要在我的适配器中覆盖getDropDownView以及getView

Having to override toString() is symptomatic that you haven't overridden the right methods in your adapter. 必须覆盖toString()是有症状的,你没有覆盖适配器中的正确方法。

When overriding getDropDownView having to override toString() is no longer required and everything works as expected. 当重写getDropDownView不得不重写toString()时,一切都按预期工作。

And the answer on the following post is a great way to implement by using the super method: alternating colors of spinner items 以下帖子的答案是使用super方法实现的一种很好的方法: 交替旋转颜色的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM