How to use spinner and fill it from array in android
I am new to Android Development and I want to implement Spinner
. My problem is that I have an array that has 9 values for a key category. My array looks like this:
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 a category key. I need MyArray for spinner like below:
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 idea?
Please help me.
thank
source to share
first create xml string for 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 for string array in your code like 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);
}
source to share
try this way, it will work fine.
public class ArrayAdapterDemo2 extends Activity implements
AdapterView.OnItemSelectedListener {
TextView selection;
String[] category= { "football","cricket" ,"baseball"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item, category);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
selection.setText(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
}
source to share
If I understand your question correctly, it will be sol 1. Create a Spinner Spinner spinner;
2.Create data String[] category={"Cricket","football".....};
3.spinner.setAdapter (new Arrayadapter (this, android.R.layout.simple_spinner_item, category));
4.select a specific value on the counter to get the value
spinner.setOnItemSelected(new onItemselected(public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// to get selected Value
Toast.makeText(getApplicationContext(),"selected:"+category[pos],5000).show();
});
source to share
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
public class AndroidSQLite extends Activity implements OnItemSelectedListener {
Spinner spinnergame;
String[] games = { "football","cricket" ,"baseball"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.creategame);
spinnergame = (Spinner) findViewById(R.id.game);
spinnergame.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, games);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercity.setAdapter(aa);
public String getSpinnerSelection() {
return this.selectedspinnervalue;
}
public void setSpinnerSelection(String selection) {
this.selectedspinnervalue = selection;
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
selection.setText("");
}
}
source to share