Harsh
Harsh

Reputation: 37

Android spinner default value problem

In my Application i am using Spinner which contain 1 to 60 items from which user have to select anyone of them.My problem is that when user again call the Activity which contain spinner, user can able to see his previously selected item as the default value of spinner. I have try to use the spineer.setselection() but i am not able to get. so if anyone have any ideal about this then please give some hint to solve my problem.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup viewgroup = (ViewGroup) findViewById(R.id.myprofilelinear);
    ViewGroup.inflate(this, R.layout.spinner,
            viewgroup);

    LoadUI();
}
public void LoadUI()
{
    imgtoggle = (ImageView) findViewById(R.id.btntoggle);
    imgtoggle.setOnClickListener(this);
    String param=JsonDataProcessor.MYPROFILELIST.get(0).getIsTracking();
    if(param.equalsIgnoreCase("True"))
    {
        imgtoggle.setImageResource(R.drawable.on_button);
    }
    else
    {
        imgtoggle.setImageResource(R.drawable.off_button);
    }

    btnsave = (Button)findViewById(R.id.btnSavechanges);
    btnsave.setOnClickListener(this);

    String[] s = new String[60];
    int i;
    for (i = 0; i < 60; i++) {
        s[i] = Integer.toString(i+1);
    }

     spin = (Spinner) findViewById(R.id.TimeSpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(TimeSpin.this,
            android.R.layout.simple_spinner_item, s);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(adapter);

}

Upvotes: 0

Views: 7169

Answers (2)

Arun Badole
Arun Badole

Reputation: 11097

Use

spin.setSelection(adapter.getPosition(previously_saved_value));

previously_saved_value can be get by using Shared Preference or by making a static variable and its value is set to what user has previously selected.

Upvotes: 8

Venky
Venky

Reputation: 11107

You should Ignite OnItemselectedListener for this and set the selected value in Spinner

Check this Code :

    boolean flag_default_country=false;

    spin_country.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
                for (int i = 0; i < s.length; i++) {
                    if (flag_default_country == false) {
                        if (s[i].toString().equalsIgnoreCase("US")) {
                            spin_country.setSelection(i);
                            flag_default_country = true;
                        }
                    }
                }
            }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

spin_country is Spinner Object

first initialize the flag_default_country to false , then check for the String value selected by User that was stored in Some values. if the Array value at i'th position equals to the user selected value it will set the value at i in the Spinner.

For Saving the user selected value you can use the Shared preference.

Check this link for Shared Preference.

Let me know if you find any difficulty.

Upvotes: 1

Related Questions