newdroiddev
Lurker
Hi,
I need a little help with spinners. I am trying to get spinner in a section in the drawer. I cannot populate the spinner using the array adapters, unless i do the below in my xml file.
android:entries="@array/spinner_info"
given below is my code snippet
public class SpinnerActivity extends BaseActivity {
private String[] accounts;
private Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
accounts = getResources().getStringArray(R.array.spinner_info);
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, accounts);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
int pos= spinner.getSelectedItemPosition();
selectItem(pos);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void selectItem(int pos)
{
switch (pos) {
case 0:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
break;
case 1:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
break;
case 2:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
default:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
}
}
}
If i populate the spinner using android:entries="@array/spinner_info" in my xml, the spinner gets populated but the items selection does not work.
I would be very thanksful for some advices. thanks in advance
I need a little help with spinners. I am trying to get spinner in a section in the drawer. I cannot populate the spinner using the array adapters, unless i do the below in my xml file.
android:entries="@array/spinner_info"
given below is my code snippet
public class SpinnerActivity extends BaseActivity {
private String[] accounts;
private Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
accounts = getResources().getStringArray(R.array.spinner_info);
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, accounts);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
int pos= spinner.getSelectedItemPosition();
selectItem(pos);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void selectItem(int pos)
{
switch (pos) {
case 0:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
break;
case 1:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
break;
case 2:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
default:
Toast.makeText(this, R.string.nothing_to_display, Toast.LENGTH_SHORT)
.show();
}
}
}
If i populate the spinner using android:entries="@array/spinner_info" in my xml, the spinner gets populated but the items selection does not work.
I would be very thanksful for some advices. thanks in advance