简体   繁体   中英

Get selected entries from MultiSelectlList [android]

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM