Example usage for android.preference PreferenceScreen getContext

List of usage examples for android.preference PreferenceScreen getContext

Introduction

In this page you can find the example usage for android.preference PreferenceScreen getContext.

Prototype

public Context getContext() 

Source Link

Document

Returns the android.content.Context of this Preference.

Usage

From source file:de.badaix.snapcast.GroupSettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.group_preferences);
    PreferenceScreen screen = this.getPreferenceScreen();

    prefStreams = (ListPreference) findPreference("pref_stream");

    Bundle bundle = getArguments();/*from  w ww . j av a 2 s. co m*/
    try {
        group = new Group(new JSONObject(bundle.getString("group")));
        serverStatus = new ServerStatus(new JSONObject(bundle.getString("serverStatus")));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    final ArrayList<Stream> streams = serverStatus.getStreams();
    final CharSequence[] streamNames = new CharSequence[streams.size()];
    final CharSequence[] streamIds = new CharSequence[streams.size()];
    for (int i = 0; i < streams.size(); ++i) {
        streamNames[i] = streams.get(i).getName();
        streamIds[i] = streams.get(i).getId();
    }

    prefStreams.setEntries(streamNames);
    prefStreams.setEntryValues(streamIds);

    for (int i = 0; i < streams.size(); ++i) {
        if (streamIds[i].equals(group.getStreamId())) {
            prefStreams.setTitle(streamNames[i]);
            prefStreams.setValueIndex(i);
            oldStream = prefStreams.getValue();
            break;
        }
    }

    prefStreams.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            for (int i = 0; i < streams.size(); ++i) {
                if (streamIds[i].equals(newValue)) {
                    prefStreams.setTitle(streamNames[i]);
                    //                        client.getConfig().setStream(streamIds[i].toString());
                    prefStreams.setValueIndex(i);
                    break;
                }
            }

            return false;
        }
    });

    prefCatClients = (PreferenceCategory) findPreference("pref_cat_clients");
    ArrayList<CheckBoxPreference> allClients = new ArrayList<>();
    for (Group g : serverStatus.getGroups()) {
        for (Client client : g.getClients()) {
            CheckBoxPreference checkBoxPref = new CheckBoxPreference(screen.getContext());
            checkBoxPref.setKey(client.getId());
            checkBoxPref.setTitle(client.getVisibleName());
            checkBoxPref.setChecked(g.getId().equals(group.getId()));
            checkBoxPref.setPersistent(false);
            allClients.add(checkBoxPref);
        }
    }
    Collections.sort(allClients, new Comparator<CheckBoxPreference>() {
        @Override
        public int compare(CheckBoxPreference lhs, CheckBoxPreference rhs) {
            return lhs.getTitle().toString().compareToIgnoreCase(rhs.getTitle().toString());
        }
    });
    for (CheckBoxPreference pref : allClients)
        prefCatClients.addPreference(pref);

    oldClients = getClients().toString();
}