Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set spinner values runtime in Android

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?

like image 567
dominic Avatar asked Jul 25 '12 18:07

dominic


People also ask

How do you refresh a spinner?

notifyDataSetChanged(); The notifyDataSetChanged() will force refresh the spinner so that the list change can be applied to the view.

What is an ArrayAdapter in Android?

ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView.


2 Answers

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.

like image 139
MH. Avatar answered Sep 17 '22 12:09

MH.


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                   
        }
    });               
}
like image 24
Balaji Avatar answered Sep 20 '22 12:09

Balaji