How to use the spinner and populate it from an array in Android
•
Android
I am a novice in Android development. I want to implement spinner. My problem is that I have an array with a key category value of 9. My array looks like:
MyArray
[{Category=Things To Do},
{Category=Transportation},
{Category=Food and Drink},
{Category=Accommodation},
{Category=Shopping},
{Category=Money and Costs},
{Category=Business},
{Category=Turkey Tour},
{Category=Events}]
I want to get the value of category keys. I need myArray for spinner as follows:
MyArray
{
Things To Do
Transportation
Food and Drink
Accommodation
Shopping
......
}
It is similar to the iPhone code
// iPhone code
for(int i=0; i<[arrayCategory count]; i++)
{
NSString *strSubTitle=[[arrayCategory objectAtIndex:i]objectForKey:@"category"];
}
Any ideas?
Please help me
thank you
resolvent:
First, create an XML in the string of the string array
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
Then use the above XML as a string array in the code, as shown below
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码