我想用键值实现一个android微调器。
zh:english jp:japanese
当用户选择和项目我想将密钥保存到数据库时。而且在下一次加载时我想要微调器选择微粒旋转器位置。
任何人都可以告诉我实现这个的最佳方法是什么。
我试过以下但没有达到我的要求 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的项目
答案 0 :(得分:11)
设置Spinner:
spLang = (Spinner)view.findViewById( R.id.spLang );
spLang.setOnItemSelectedListener( this );
ArrayList<String> sp_Lang = new ArrayList<String>();
sp_Lang.add("english");
sp_Lang.add("french");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sp_Lang);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLang.setAdapter(spinnerAdapter);
对于Perticular Spinner项目选择:
int Position = spinnerAdapter.getPosition("Japanese");
spLang.setSelection(Position);
首先为商店密钥和值对创建Hashmap
HashMap<String ,String> hmLang = new HashMap<String,String>();
现在将这样的值添加到HashMap中:
hmLang.put("english" ,"en");
这里有HashMap key = YourValue和Value = yourKey
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
switch(adapterView.getId())
{
case R.id.spLang:
String lang_Name = adapterView.getItemAtPosition(position).toString();
String lang_Key = hmLang.get(lang_Name);
break
}
如果您对此代码有任何疑问,请询问
答案 1 :(得分:2)
我使用HashMap适配器创建了这些场景。另请参见示例项目here
mapData = new LinkedHashMap<String, String>();
mapData.put("shamu", "Nexus 6");
mapData.put("fugu", "Nexus Player");
mapData.put("volantisg", "Nexus 9 (LTE)");
mapData.put("volantis", "Nexus 9 (Wi-Fi)");
mapData.put("hammerhead", "Nexus 5 (GSM/LTE)");
mapData.put("razor", "Nexus 7 [2013] (Wi-Fi)");
mapData.put("razorg", "Nexus 7 [2013] (Mobile)");
mapData.put("mantaray", "Nexus 10");
mapData.put("occam", "Nexus 4");
mapData.put("nakasi", "Nexus 7 (Wi-Fi)");
mapData.put("nakasig", "Nexus 7 (Mobile)");
mapData.put("tungsten", "Nexus Q");
adapter = new LinkedHashMapAdapter<String, String>(this, android.R.layout.simple_spinner_item, mapData);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
答案 2 :(得分:1)
您可以在数据库中保存选定的spinner
项目位置,然后下次按保存的位置设置spinner
项目
获得位置:
int position = spinner1.getSelectedItemPosition();
用于设置:
spinner.setSelection(position);
请注意,在设置setSelection()
spinner
adapter
方法
答案 3 :(得分:0)
答案 4 :(得分:0)
填充微调器
void populateRecordPerPageSpinner(LinkedTreeMap<String, String> map) {
String selectedValue = PreferencesHelper.getSharedPreferenceString(context, Constants.PREF_RECORD_PER_PAGE_KEY);
List<String> recordPerPageList = new ArrayList();
for (Map.Entry<String, String> val : map.entrySet()) {
int resourceId = getResources().getIdentifier(val.getValue(), "string", getPackageName());
recordPerPageMap.put(val.getKey(), getResources().getString(resourceId));
recordPerPageList.add(getResources().getString(resourceId));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.spinner_layout, recordPerPageList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mRecordPerPage.setAdapter(arrayAdapter);
mRecordPerPage.setSelection(arrayAdapter.getPosition(selectedValue));
}
优先获取并保存位置
selectedRecPerPage = recordPerPageMap.get(mRecordPerPage.getAdapter().getItem(recordPerPagePosition));
PreferencesHelper.setSharedPreferenceString(context, Constants.PREF_RECORD_PER_PAGE_KEY, selectedRecPerPage);
答案 5 :(得分:0)
我已经更新了 Dhaval 的答案,使其更通用。此解决方案使用 toString()
和 ArrayAdapter
(您不需要使用所有逻辑实现自己的适配器)并在 Kotlin
创建您的自定义类并覆盖 toString()
方法。
data class SpinnerEntry(
// define whatever properties you want
val label: String,
val key: MyEnum,
) {
override fun toString(): String {
return label // this will display to user
}
}
初始化微调器
val entries = listOf(
// you want to extract string resources to XML
SpinnerEntry("LabelA", MyEnum.A),
SpinnerEntry("LabelB", MyEnum.B),
SpinnerEntry("LabelC", MyEnum.C),
SpinnerEntry("LabelD", MyEnum.D),
)
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
val selectedItem = parent?.getItemAtPosition(position) as SpinnerEntry
val key = selectedItem.key
// do something with selected key
}
override fun onNothingSelected(parent: AdapterView<*>?) {
// do something when unselected
}
}
val spinnerAdapter: ArrayAdapter<SpinnerEntry> = ArrayAdapter(
applicationContext!!, // get context depending on your implementation
android.R.layout.simple_spinner_item,
entries
)
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = spinnerAdapter