【发布时间】:2016-05-28 17:59:48
【问题描述】:
我想实现一个带有键值的 android spinner。
zh:英语 jp:日语
当用户选择和项目时,我想将密钥保存到数据库。而且在下一次加载时,我希望微调器选择一个微粒微调器位置。
谁能告诉我实现这一点的最佳方法是什么。
我已尝试关注但不符合我的要求 https://gist.github.com/granoeste/2786663 Set Key and Value in spinner
现在写我正在使用 LinkedHashMapAdapter。但问题是,在这里我需要将位置存储在 DB 或本地共享首选项中。我不认为这是一个更好的解决方案
当前实现
LinkedHashMap localeList = new LinkedHashMap<>();
for (Map.Entry<String, String> val : map.entrySet()) {
int resourceId = getResources().getIdentifier(val.getValue(), "string", getPackageName());
localeList.put(val.getKey(), getResources().getString(resourceId));
}
LinkedHashMapAdapter<String, String> arrayAdapter = new LinkedHashMapAdapter<>(this, R.layout.spinner_layout, localeList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mLocale.setAdapter(arrayAdapter);
String localPosSelected = PreferencesHelper.getSharedPreferenceString(this, Constants.PREF_LOCALE_POS_KEY);
if(localPosSelected!=null){
mLocale.setSelection(Integer.parseInt(localPosSelected));
}
还有一个选项可以按值选择 Spinner 的项目
【问题讨论】: