Hello All,

Here is my dilemma. I have a spinner with items that correspond to values that will be used in a calculation depending on which spinner item is selected. I also have a button that will bring up a list of the items that a re in the spinner so that a user can choose one as the default. What I need to do is change the spinner to that default value when the app is launched. Any help will be greatly appreciated. I am not sure how much code you need to be included so let me know if you need more. I understand that I am using deprecated code. I was going to deal with that later.

MainActivity.java
package droid.fuelConverter;
 
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
	EditText gallons;
	EditText pounds;
	Button clear;
	Button convert;
	Button setDefaultFuelGrade;
	Button viewDefaultFuelGrade;
	Spinner fuelGrade;
	RadioButton gallonsToPounds;
	RadioButton poundsToGallons;
	TextView prefResult;
 
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        prefResult = (TextView)findViewById(R.id.tvPreferenceResult);
 
        gallons = (EditText)findViewById(R.id.etGallons);
 
        pounds = (EditText)findViewById(R.id.etPounds);
 
        clear = (Button)findViewById(R.id.btnClear);
        clear.setOnClickListener(new OnClickListener() {
 
        	// Clear button
			public void onClick(View v) {
				clear();
			}
		});
 
        convert = (Button)findViewById(R.id.btnConvert);
        convert.setOnClickListener(new OnClickListener() {
 
        	// Convert button
			public void onClick(View v) {
				convert();
			}
		});
 
        setDefaultFuelGrade = (Button)findViewById(R.id.btnSetDefaultFuelGrade);
        setDefaultFuelGrade.setOnClickListener(new OnClickListener() {
 
        	// Set default fuel grade button
			public void onClick(View v) {
				switch(v.getId()) {
				case R.id.btnSetDefaultFuelGrade:
					Intent i = new Intent(MainActivity.this, PrefActivity.class);
					startActivity(i);
					break;
				default:
					break;
				}
			}
		});
 
        // View default fuel grade button
        viewDefaultFuelGrade = (Button)findViewById(R.id.btnViewDefaultFuelGrade);
        viewDefaultFuelGrade.setOnClickListener(new OnClickListener() {
 
			public void onClick(View v) {
				setDefaultFuelGrade();
			}
		});
 
        fuelGrade = (Spinner)findViewById(R.id.spinnerFuelType);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
        		(this, R.array.FuelType, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fuelGrade.setAdapter(adapter);
 
        gallonsToPounds = (RadioButton)findViewById(R.id.radioGallonsToPounds);
        gallonsToPounds.setChecked(true);
 
        poundsToGallons = (RadioButton)findViewById(R.id.radioPoundsToGallons);
        poundsToGallons.setChecked(false);
    }
 
	private void setDefaultFuelGrade() {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    	String listPrefs = prefs.getString("fuelGradePreference", "");
    	StringBuilder builder = new StringBuilder();
    	builder.append("List perference: " + listPrefs);
    	prefResult.setText(builder.toString());
	}
 
	/**
	 * Method to clear and reset the converter
	 */
    protected void clear() {
    	gallons.setText("");
    	pounds.setText("");
    	gallonsToPounds.setChecked(true);
    	poundsToGallons.setChecked(false);
	}
 
	/**
	 * Method to assign spinner value to corresponding array value and convert
	 */
    protected void convert() {
    	double fuelWeightArray[] = {6.4, 6.8, 6.7, 6.7, 6.8, 6.4, 6.7, 6.0};
    	int fuelWeightValue = fuelGrade.getSelectedItemPosition();
 
    	if(gallonsToPounds.isChecked() && gallons.getText().toString().equals("")) {
    		Toast.makeText(getBaseContext(), "Enter gallon amount", Toast.LENGTH_SHORT).show();
 
    	} else if(poundsToGallons.isChecked() && pounds.getText().toString().equals("")) {
    		Toast.makeText(getBaseContext(), "Enter pound amount", Toast.LENGTH_SHORT).show();
 
    	} else {
        	if(gallonsToPounds.isChecked()) {
        		double value = Double.parseDouble(gallons.getText().toString());
        		pounds.setText(Double.toString(value * fuelWeightArray[fuelWeightValue]));
        	}
 
        	if(poundsToGallons.isChecked()) {
        		double value = Double.parseDouble(pounds.getText().toString());
            	gallons.setText(Double.toString(value / fuelWeightArray[fuelWeightValue]));
            }
    	}
    }
 
    /**
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    	case R.id.meunAbout:
    		return true;
    	case R.id.menuSettings:
    		return true;
    	}
    	return false;
    }
    */
}

PrefActivity.java
package droid.fuelConverter;
 
import android.os.Bundle;
import android.preference.PreferenceActivity;
 
public class PrefActivity extends PreferenceActivity {
 
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}
 
}

string.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
 
    <string name="app_name">Fuel Converter</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="tvFuelGallons">Fuel Gallons</string>
    <string name="tvFuelPounds">Fuel Pounds</string>
    <string name="menuAbout">About</string>
    <string name="menuSettings">Settings</string>
    <string name="fuelType_array">Fuel Type</string>
    <string name="btnClear">Clear</string>
    <string name="btnSetDefaultFuelGrade">Set Fuel Grade</string>
    <string name="btnViewDefaultFuelGrade">View Fuel Grade</string>
    <string name="btnConvert">Convert</string>
    <string name="radioPoundsToGallons">Convert Pounds To Gallons</string>
    <string name="radioGallonsToPounds">Conver Gallons To Pounds</string>
 
    <string-array name="FuelType">
        <item>JP-4</item>
        <item>JP-5</item>
        <item>JP-8</item>
        <item>JET A</item>
        <item>JET A-1</item>
        <item>JET B</item>
        <item>TS1</item>
        <item>100/100LL</item>
    </string-array>
 
</resources>

preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <PreferenceCategory >
 
        <ListPreference
            android:title="Default Fuel Grade"
        	android:summary="Select a default fuel grade"
        	android:key="fuelGradePreference"
        	android:defaultValue="0"
        	android:entries="@array/FuelTypeEntries"
        	android:entryValues="@array/FuelTypeValues" />
 
    </PreferenceCategory>
 
</PreferenceScreen>

array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string-array name="FuelTypeEntries">
        <item>JP-4</item>
        <item>JP-5</item>
        <item>JP-8</item>
        <item>JET A</item>
        <item>JET A-1</item>
        <item>JET B</item>
        <item>TS1</item>
        <item>100/100LL</item>
    </string-array>
 
    <string-array name="FuelTypeValues">
        <item>JP-4 has been selected</item>
        <item>JP-5 has been selected</item>
        <item>JP-8 has been selected</item>
        <item>JET A has been selected</item>
        <item>JET A-1 has been selected</item>
        <item>JET B has been selected</item>
        <item>TS1 has been selected</item>
        <item>100/100LL has been selected</item>
    </string-array>
 
</resources>