Reputation: 133
I want to get selected entries (not entryValue) from MultiSelectList. For example if values 1 is selected i want to get "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);
}
Upvotes: 1
Views: 1625
Reputation:
You can extend MultiSelectListPreference
and add a method for this:
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. It returns the current entries as a list sorted as the original entry list.
Upvotes: 1
Reputation: 453
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);
}
Upvotes: 1