0

我有四个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();

活动一:

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);
}
4

1 回答 1

0

我建议您查看Spinners 教程。似乎您需要为 Spinner 创建适当的Adapter(我认为ArrayAdapter足以满足您的目的)并对其进行更新,然后您需要更新 Spinner 中的数据。

您可以在Activity 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);
于 2013-09-23T06:56:49.453 回答