How to set spinner values runtime in Android
Asked Answered
C

3

8

I want to set the android spinner item values at runtime.

Here is what I have so far:

final ArrayAdapter<String> calsListAdapter = new ArrayAdapter<String>( this,
                android.R.layout.simple_list_item_1, calendarNames);
eventCalendarList.setAdapter(calsListAdapter);
eventCalendarList.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        calendarChoosen = availableCals.get(arg2);
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        Logger.d("Cal Choosen", "fffffffffffffff");
    }
});

private List<AvailableCalendar> availableCals = new ArrayList<AvailableCalendar>();
private AvailableCalendar calendarChoosen;

But the values are not set. How can it be done?

Clear answered 25/7, 2012 at 18:20 Comment(3)
Post some code. Your explanation isn't clear enough to understand the problem.Supporting
@DavidWasser The code is as follows:final ArrayAdapter<String> calsListAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, calendarNames); eventCalendarList.setAdapter(calsListAdapter); eventCalendarList .setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { calendarChoosen = availableCals.get(arg2); } public void onNothingSelected(AdapterView<?> arg0) { Logger.d("Cal Choosen", "fffffffffffffff"); } });Clear
private List<AvailableCalendar> availableCals = new ArrayList<AvailableCalendar>(); private AvailableCalendar calendarChoosen;Clear
P
11

I'm going to assume here that you create the Spinner's adapter based on a List<CharSequence> or something similar. You can use that to do lookups for selection, e.g.:

String name = model.getName();
int index = list.indexOf(name);
if (index != -1) spinner.setSelection(index);

Obviously, if your model does not contain any 'name' data, then there's nothing to select in the Spinner, so you may want to add some logic to handle that. If the model does have a 'name', then find its index in the original list that was used as data source for the adapter. Only if an occurrence was found, set the selection of the spinner to that same index.

Pfaff answered 25/7, 2012 at 19:39 Comment(0)
E
3

I set the spinner values programatically like this:

public class MainActivity extends Activity {
    private SharedPreferences prefs;
    private String prefName = "spinner_value";
    int id=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final List<String> list=new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");

        final Spinner sp=(Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adp= new ArrayAdapter<String>(this,
                                    android.R.layout.simple_list_item_1,list);
        adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adp);

        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        id=prefs.getInt("last_val",0);
        sp.setSelection(id);

        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, 
                View arg1,int pos, long arg3) {

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            //---save the values in the EditText view to preferences---
            editor.putInt("last_val", pos);

            editor.commit();

            Toast.makeText(getBaseContext(), 
                sp.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub                   
        }
    });               
}
Employment answered 30/7, 2012 at 23:44 Comment(0)
M
0

in kotlin it will be like this:

spinner.adapter = ArrayAdapter(
            context,
            android.R.layout.simple_list_item_1,
            stringList
)
Melvin answered 8/9, 2020 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.