I have tried to use the examples and read those links that I think related to my project, but all of it have a different structure from mine. Hope anybody could help.
I have 3 spinners in my NewProfile.java class. In this class, user can input their profiles and also selecting the type of workout with the user interests. I have no problem saving the profile text input. But I have a problem to save the spinner value selected by the user. Following are my codes.
NewProfile.java
public class NewProfile extends Activity {
EditText profileName, profileDOB, profileSex, profileWeight, profileHeight;
Spinner workoutspinner, levelspinner, weightplan1, weightplan2,
weightplan3;
TextView display;
DBController controller = new DBController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newdata);
profileName = (EditText) findViewById(R.id.etName);
profileDOB = (EditText) findViewById(R.id.etDOB);
profileSex = (EditText) findViewById(R.id.etSex);
profileWeight = (EditText) findViewById(R.id.etWeight);
profileHeight = (EditText) findViewById(R.id.etHeight);
chooseWorkout();
chooseLevel();
chooseWeight1();
chooseWeight2();
chooseWeight3();
}
public void addNewProfile(View view) {
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
queryValuesMap.put("profileName", profileName.getText().toString());
queryValuesMap.put("profileDOB", profileDOB.getText().toString());
queryValuesMap.put("profileSex", profileSex.getText().toString());
queryValuesMap.put("profileWeight", profileWeight.getText().toString());
queryValuesMap.put("profileHeight", profileHeight.getText().toString());
controller.insertProfile(queryValuesMap);
this.startingpointActivity(view);
}
private void startingpointActivity(View view) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getApplicationContext(),
StartingPoint.class);
startActivity(objIntent);
finish();
}
private void chooseWorkout() {
// TODO Auto-generated method stub
workoutspinner = (Spinner) findViewById(R.id.spinWorkout);
ArrayAdapter<CharSequence> workoutadapter = ArrayAdapter
.createFromResource(this, R.array.saWorkout,
android.R.layout.simple_spinner_item);
workoutadapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
workoutspinner.setAdapter(workoutadapter);
workoutspinner.setOnItemSelectedListener(new workoutOnClickListener());
}
private void chooseLevel() {
// TODO Auto-generated method stub
levelspinner = (Spinner) findViewById(R.id.spinLevel);
ArrayAdapter<CharSequence> leveladapter = ArrayAdapter
.createFromResource(this, R.array.saLevel,
android.R.layout.simple_spinner_item);
leveladapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
levelspinner.setAdapter(leveladapter);
levelspinner.setOnItemSelectedListener(new levelOnClickListener());
}
private void chooseWeight1() {
// TODO Auto-generated method stub
weightplan1 = (Spinner) findViewById(R.id.spinWeight);
ArrayAdapter<CharSequence> weightadapter1 = ArrayAdapter
.createFromResource(this, R.array.saWeight1,
android.R.layout.simple_spinner_item);
weightadapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightplan1.setAdapter(weightadapter1);
}
private void chooseWeight2() {
// TODO Auto-generated method stub
weightplan2 = (Spinner) findViewById(R.id.spinWeight);
ArrayAdapter<CharSequence> weightadapter2 = ArrayAdapter
.createFromResource(this, R.array.saWeight2,
android.R.layout.simple_spinner_item);
weightadapter2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightplan2.setAdapter(weightadapter2);
}
private void chooseWeight3() {
// TODO Auto-generated method stub
weightplan3 = (Spinner) findViewById(R.id.spinWeight);
ArrayAdapter<CharSequence> weightadapter3 = ArrayAdapter
.createFromResource(this, R.array.saWeight3,
android.R.layout.simple_spinner_item);
weightadapter3
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightplan3.setAdapter(weightadapter3);
}
public class workoutOnClickListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
parent.getItemAtPosition(pos);
if (pos == 0) {
chooseLevel();
levelspinner.setClickable(true);
weightplan1.setClickable(true);
weightplan2.setClickable(true);
weightplan3.setClickable(true);
} else if (pos != 0){
levelspinner.setClickable(false);
weightplan1.setClickable(false);
weightplan2.setClickable(false);
weightplan3.setClickable(false);
Toast.makeText(getApplicationContext(),
"Wollaaaa! Only Program 1 is available for this moment. Sorry.. :)",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
public class levelOnClickListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
// TODO Auto-generated method stub
parent.getItemAtPosition(pos);
if (pos == 0) {
chooseWeight1();
} else if (pos == 1){
chooseWeight2();
} else if (pos == 2){
chooseWeight3();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
Do I need to change anything in my DBController in order to save these spinners? Please help. Thanks.
p/s Just to inform that the addNewProfile is a button that save all the information.