1

我想从 MultiSelectList 中获取选定的条目(不是 entryValue)。例如,如果选择值 1,我想得到“546544654”。现在我得到了价值观。你能帮助我吗?

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

2 回答 2

1

当点击列表视图时,您可以通过它的位置找到它

public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

String val =(String) parent.getItemAtPosition(position);
System.out.println("Value is "+val); 
  }
于 2014-05-28T06:45:27.767 回答
1

您可以为此扩展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;
    }
}

注意:这是 O(|E||C|),其中 E 是条目集,C 是当前条目集。它将当前条目作为按原始条目列表排序的列表返回。

于 2017-01-19T14:50:13.283 回答