[英]Get selected entries from MultiSelectlList [android]
I want to get selected entries (not entryValue) from MultiSelectList. 我想从MultiSelectList获取选定的条目(而不是entryValue)。 For example if values 1 is selected i want to get "546544654".
例如,如果选择值1,我想得到“ 546544654”。 Now i get the Values.
现在我得到了价值观。 Can you help me?
你能帮助我吗?
MultiSelectListPreference list = (MultiSelectListPreference) findPreference("multiselectlist");
CharSequence[] entries = { "546544654", "12312", "98987","4342423","432423432" };
CharSequence[] entryValues = { "1", "2", "3","4","5" };
list.setEntries(entries); // entries type is String[]
list.setEntryValues(entryValues); // entryValues type is String[]
Set<String> selections = sharedPrefs.getStringSet("multiselectlist", null);
for (String str: selections){
Log.d("salida", str);
}
you can find it by its position ,when click on listview 单击列表视图时,可以按其位置找到它
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String val =(String) parent.getItemAtPosition(position);
System.out.println("Value is "+val);
}
You can extend MultiSelectListPreference
and add a method for this: 您可以扩展
MultiSelectListPreference
并为此添加方法:
import android.content.Context;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class MultiSelectListPreference extends android.preference.MultiSelectListPreference {
public MultiSelectListPreference(Context context) {
super(context);
}
public MultiSelectListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public List<String> getCurrentEntries() {
CharSequence[] entries = getEntries();
CharSequence[] entryValues = getEntryValues();
List<String> currentEntries = new ArrayList<>();
Set<String> currentEntryValues = getValues();
for (int i = 0; i < entries.length; i++)
if (currentEntryValues.contains(entryValues[i]))
currentEntries.add(entries[i].toString());
return currentEntries;
}
}
Note: this is O(|E||C|), where E is the set of entries and C is the set of current entries. 注意:这是O(| E || C |),其中E是条目集,C是当前条目集。 It returns the current entries as a list sorted as the original entry list.
它以列表的形式返回当前条目,该列表按原始条目列表排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.