保存微调器并将其用于其他活动

时间:2014-04-03 16:53:54

标签: android save spinner

我尝试使用这些示例并阅读我认为与我的项目相关的链接,但所有这些链接都与我的结构不同。希望有人能提供帮助。

我的NewProfile.java类中有3个微调器。在这个课程中,用户可以输入他们的个人资料,还可以选择符合用户兴趣的锻炼类型。保存配置文件输入没有问题。但是我有一个问题是保存用户选择的微调器值。以下是我的代码。

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

    }

}
}

我是否需要更改DBController中的任何内容才能保存这些微调器?请帮忙。感谢。

p / s只是告知addNewProfile是一个保存所有信息的按钮。

1 个答案:

答案 0 :(得分:0)

好的,我会尽力按要求回答:

首先,这些是设置和获取微调器值的关键部分: 如果您需要在微调器中设置值,可以使用它。

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

从你的微调器中获取值。

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

以下是关于如何处理这些价值观的精彩讨论。 SharedPreferences example

这很好地解释了如何保存您的偏好。它也应该给你几乎所有的东西来回答你的初始问题。

之后的关键部分是如何处理这些prefs。此行显示如何根据在已保存的prefs中找到的内容设置字符串,如果找不到任何内容,则会加载“default”。 当用户第一次运行应用程序并且从未设置过prefs时,这很有用。

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

一旦设置了这些首选项,并且您希望它们显示在不同的视图中,只需将所有这些相同的技术添加到另一个视图中。

希望有所帮助。对于它的价值,最后一点是从这里,通过各种搜索。有时,如果一个简单的解决方案不在一个地方,你需要查看一堆不同的东西才能找到答案。