将edittext值传递给微调器

时间:2013-09-21 15:02:08

标签: android hashmap sharedpreferences android-spinner

我有四个EditText值。我使用SharedPreferences存储值以便稍后获取。存储后我需要通过点击微调器来获取这些值。我搜索了一些文件,但没有清楚地理解。我只需要从edittext获取et值,如果我在edittext中重新输入另一个值,我需要用2个按钮增加微调器值。

代码:

的活动:

final Map<String, String> demomap = new HashMap<String, String>();

demomap.put(et.getText().toString(), et1.getText().toString());
demomap.put(et2.getText().toString(), et3.getText().toString());
SharedPreferences preferences = getSharedPreferences("CREATE_MEAL",
        0);
SharedPreferences.Editor editor = preferences.edit();
for (Entry<String, String> entry : demomap.entrySet()) {
    editor.putString(entry.getKey(), entry.getValue());
}
editor.commit();

活动1:

spinner = (Spinner)findViewById(R.id.spinner1);
demomap = new HashMap<String, String>();
SharedPreferences preferences = getSharedPreferences("CREATE_MEAL", 0);
for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
    demomap.put(entry.getKey(), entry.getValue().toString());
}

Iterator myVeryOwnIterator = demomap.keySet().iterator();

while (myVeryOwnIterator.hasNext()) {
    String key = (String) myVeryOwnIterator.next();
    String value = (String) demomap.get(key);

    spinner.setText(value);
}

1 个答案:

答案 0 :(得分:0)

我建议您查看Spinners tutorial。看起来你需要为Spinner创建适当的Adapter(我认为ArrayAdapter就足够了)并更新它,然后你需要在Spinner中更新数据。

您可以在活动1 中执行以下操作:

    Spinner spinner = (Spinner)findViewById(R.id.spinner1);
    demomap = new ArrayList<String>();

    SharedPreferences preferences = getSharedPreferences("CREATE_MEAL", 0);
    for (Map.Entry<String, ?> entry : preferences.getAll().entrySet()) {
        demomap.add(entry.getValue().toString());
    }

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, demomap);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);