NullPointerException
NullPointerException

Reputation: 37701

How to use a personalized spinner with programatically created array of Strings instead of a hard-coded string array of my strings.xml file?

i have a personaliced spinner (that uses a adapter that uses a personalized xml layout file)

i want to pass him a array of strings, programatically created, instead of passing the tipical hard coded string array of strings.xml... but i can't!! if i try to pass a array of strings, eclipse tell me that he needs a reference of a item from resources (a hard coded string array from strings.xml)

also i tryed to use the answer of this another way: Android: Create spinner programmatically from array

but that way didn't work's for me, because that way must use the default layout android.R.layout.simple_spinner_item and i won't work with that layout, i need to use my personalized layout (R.layout.spinner_layout), and that method didn't let me to use personalized layouts for the spinner

this is my code:

String[] teams=(String[])Primera.getTeams().toArray();

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.teams_array, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
TeamsSpinner.setAdapter(adapter);
TeamsSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

i need to changue R.array.teams_array, to teams, but i can't because the reason i told some lines up

can anyone help me ?

thanks

Upvotes: 0

Views: 747

Answers (1)

citizen conn
citizen conn

Reputation: 15390

You do not have to use the android.R.layout.spinner_item etc. like what is being used in the question you reference. You can use your own layout xml for the spinner item.

You do not have to use ArrayAdapter.createFromResource() you can use:

ArrayAdapter adapter = new ArrayAdapter(this, R.id.your_custom_spinner_item, teams);

Upvotes: 2

Related Questions