0

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.

Etty
  • 45
  • 1
  • 1
  • 9
  • Not that this is a simple answer. But I have spinners in a View that need to update a widget in the same app. My solution was to write the values in SharedPreferences. Once written, the Widget can call them up. Works like a charm. – durbnpoisn Apr 03 '14 at 16:58
  • @durbnpoisn Do you have any example for this kind of activity? I just don't have any idea how to do it. Other examples are quite different. What and where should I put anything in the code to save the selected spinner. – Etty Apr 03 '14 at 17:09
  • Well, like I said, it's not simple. But I can try to explain, in short. First, you have the code where you set up your spinner, and tell it what XML to get it's values from, right? In that same spot, you can use code like this: mySpinner.setSelection(arrayAdapter.getPosition("Category 2")); String Text = mySpinner.getSelectedItem().toString(); to get and set whatever values you have. Once you have that, you can use SharedPreferences to write out the string to a named key. I got all the answers for how to do that by looking up the various stuff here. – durbnpoisn Apr 03 '14 at 17:23
  • @durbnpoisn I still don't get it. Do you have any samples that you can share? – Etty Apr 04 '14 at 12:59

1 Answers1

0

Okay, I'll do my best to answer as requested:

For a start, these are the key parts of setting and getting values from your spinners: If you need to set the values in your spinners, you can use this.

mySpinner.setSelection(arrayAdapter.getPosition("Category 2")); //This assumes that "Category 2" exists in your spinner list.

To get the value from your spinner.

String Text = mySpinner.getSelectedItem().toString(); 

Here is a great discussion on what to DO with those values. SharedPreferences example.

That pretty well explains how to save your preferences. It also should give you almost everything to answer your initial question.

The key part afterward is what to do with those prefs. This line shows how to set a string based on what it finds in the saved prefs, and if it finds nothing, will load "default". This is helpful for when the user is running the app for the first time and prefs have never been set.

test_string=shared_preferences.getString("test_key", "Default");

Once those prefs have been set, and you want them to show in a different view, just add all this same sort of technique to the other view.

Hope that helps. For what it's worth, every last bit of this was taken from here, through various searches. Sometimes you need to look through a bunch of different things to find an answer if a simple solution is not in one place.

Community
  • 1
  • 1
durbnpoisn
  • 4,666
  • 2
  • 16
  • 30