简体   繁体   中英

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. But the problem is it shows reference type data in Spinner item, see the pic

I add android.R.layout.simple_spinner_dropdown_item but not know that what to add in layout. I checked many exemples on google and they add simple_spinner_dropdown_item but i could not find that what to add in layout. 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)

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.

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. Adding toString() did fix initially until I tried to set a compound drawable in the same layout.

I needed to override getDropDownView as well as getView in my adapter.

Having to override toString() is symptomatic that you haven't overridden the right methods in your adapter.

When overriding getDropDownView having to override toString() is no longer required and everything works as expected.

And the answer on the following post is a great way to implement by using the super method: alternating colors of spinner items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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