从MultiSelectlList [android]中获取所选条目

时间:2014-05-28 06:38:02

标签: android multiselectlistpreference

我想从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);
}

2 个答案:

答案 0 :(得分:1)

点击列表视图

,您可以通过它的位置找到它
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

String val =(String) parent.getItemAtPosition(position);
System.out.println("Value is "+val); 
  }

答案 1 :(得分: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是当前条目集。它将当前条目作为列表作为原始条目列表进行返回。