下面列出了android.database.CursorIndexOutOfBoundsException#android.support.v7.preference.Preference 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
return new PreferenceGroupAdapter(preferenceScreen) {
@SuppressLint("RestrictedApi")
@Override
public void onBindViewHolder(PreferenceViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
Preference preference = getItem(position);
if (preference instanceof PreferenceCategory)
setZeroPaddingToLayoutChildren(holder.itemView);
else {
View iconFrame = holder.itemView.findViewById(R.id.icon_frame);
if (iconFrame != null) {
iconFrame.setVisibility(preference.getIcon() == null ? View.GONE : View.VISIBLE);
}
}
}
};
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
private void tintPrefIconsRecursive(PreferenceGroup prefGroup, @ColorInt int iconColor) {
if (prefGroup != null && isAdded()) {
int prefCount = prefGroup.getPreferenceCount();
for (int i = 0; i < prefCount; i++) {
Preference pref = prefGroup.getPreference(i);
if (pref != null) {
if (isAllowedToTint(pref)) {
pref.setIcon(_cu.tintDrawable(pref.getIcon(), iconColor));
}
if (pref instanceof PreferenceGroup) {
tintPrefIconsRecursive((PreferenceGroup) pref, iconColor);
}
}
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mWasPrefChanged = true;
// String stringValue = newValue.toString();
//
// if (preference instanceof ListPreference) {
// // For list preferences, look up the correct display value in
// // the preference's 'entries' list (since they have separate labels/values).
// ListPreference listPreference = (ListPreference) preference;
// int prefIndex = listPreference.findIndexOfValue(stringValue);
// if (prefIndex >= 0) {
// preference.setSummary(listPreference.getEntries()[prefIndex]);
// }
// } else {
// // For other preferences, set the summary to the value's simple string representation.
// preference.setSummary(stringValue);
// }
return true;
}
@Override
protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
return new PreferenceGroupAdapter(preferenceScreen) {
@SuppressLint("RestrictedApi")
@Override
public void onBindViewHolder(PreferenceViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
Preference preference = getItem(position);
if (preference instanceof PreferenceCategory)
setZeroPaddingToLayoutChildren(holder.itemView);
else {
View iconFrame = holder.itemView.findViewById(R.id.icon_frame);
if (iconFrame != null) {
iconFrame.setVisibility(preference.getIcon() == null
? View.GONE
: View.VISIBLE);
}
}
}
};
}
private void updateContent() {
GcmPrefs prefs = GcmPrefs.get(getContext());
for (String pref : HEARTBEAT_PREFS) {
Preference preference = findPreference(pref);
int state = prefs.getNetworkValue(pref);
if (state == 0) {
int heartbeat = prefs.getHeartbeatMsFor(preference.getKey(), true);
if (heartbeat == 0) {
preference.setSummary("ON / Automatic");
} else {
preference.setSummary("ON / Automatic: " + getHeartbeatString(heartbeat));
}
} else if (state == -1) {
preference.setSummary("OFF");
} else {
preference.setSummary("ON / Manual: " + getHeartbeatString(state * 60000));
}
}
}
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDivider(ActivityCompat.getDrawable(getActivity(), R.drawable.list_divider));
setDividerHeight(1);
PreferenceGroupAdapter adapter = (PreferenceGroupAdapter) getListView().getAdapter();
for (int i = 0; i < getListView().getAdapter().getItemCount(); i++) {//lazy global setOnPreferenceClickListener
Preference preference = adapter.getItem(i);
if (preference != null && !InputHelper.isEmpty(preference.getKey())) {
if (preference.getKey().equalsIgnoreCase("version")) {
preference.setSummary(BuildConfig.VERSION_NAME);
} else if (!(preference instanceof SwitchPreference) && !(preference instanceof ListPreference)) {
preference.setOnPreferenceClickListener(this);
}
}
}
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
// Add visualizer preferences, defined in the XML file in res->xml->pref_visualizer
addPreferencesFromResource(R.xml.pref_visualizer);
// COMPLETED (3) Get the preference screen, get the number of preferences and iterate through
// all of the preferences if it is not a checkbox preference, call the setSummary method
// passing in a preference and the value of the preference
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
PreferenceScreen preferenceScreen = getPreferenceScreen();
int count = preferenceScreen.getPreferenceCount();
for (int i = 0; i < count; i++) {
Preference pref = preferenceScreen.getPreference(i);
if (!(pref instanceof CheckBoxPreference)) {
setPreferenceSummary(pref, sharedPreferences.getString(pref.getKey(), ""));
}
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// 显示版本号
Preference version = findPreference("version");
version.setSummary(BuildConfig.VERSION_NAME);
// 打开GitHub项目地址
Preference github = findPreference("github_repo");
github.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getString(R.string.github_url)));
startActivity(intent);
return false;
}
});
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Activity activity = getActivity();
if (key.equals(getString(R.string.pref_location_key))) {
// we've changed the location
// Wipe out any potential PlacePicker latlng values so that we can use this text entry.
SunshinePreferences.resetLocationCoordinates(activity);
}
Preference preference = findPreference(key);
if (null != preference) {
if (!(preference instanceof CheckBoxPreference)) {
setPreferenceSummary(preference, sharedPreferences.getString(key, ""));
}
}
}
public static void initializeShowStatusPanel(final ListPreference listPreference)
{
listPreference.setSummary(listPreference.getEntry());
listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
{
@Override
public boolean onPreferenceChange(
Preference preference,
Object newValue)
{
preference.setSummary(listPreference.getEntries()[listPreference.findIndexOfValue(
(String) newValue)]);
return true;
}
});
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
@DexWrap
@Override
/*<7.0*/ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
//>7.0// public boolean onPreferenceTreeClick(Preference preference) {
if (Utils.isMonkeyRunning()) {
return false;
}
if (mFakeSignatureGlobalPreference != null) {
if (preference == mFakeSignatureGlobalPreference) {
writeFakeSignatureGlobalSettingWithWarningDialog(mFakeSignatureGlobalPreference.isChecked());
return false;
}
}
/*<7.0*/ return onPreferenceTreeClick(preferenceScreen, preference);
//>7.0// return onPreferenceTreeClick(preference);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Activity activity = getActivity();
if (key.equals(getString(R.string.pref_location_key))) {
// we've changed the location
// Wipe out any potential PlacePicker latlng values so that we can use this text entry.
SunshinePreferences.resetLocationCoordinates(activity);
SunshineSyncUtils.startImmediateSync(activity);
} else if (key.equals(getString(R.string.pref_units_key))) {
// units have changed. update lists of weather entries accordingly
activity.getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
}
Preference preference = findPreference(key);
if (null != preference) {
if (!(preference instanceof CheckBoxPreference)) {
setPreferenceSummary(preference, sharedPreferences.getString(key, ""));
}
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue == null || ((String)newValue).trim().length() == 0) {
return false;
}
int value;
try {
value = Integer.parseInt((String)newValue);
} catch (NumberFormatException nfe) {
Log.w(TAG, nfe);
return false;
}
if (value < 1) {
return false;
}
preference.setSummary(getResources().getQuantityString(R.plurals.ApplicationPreferencesActivity_messages_per_conversation, value, value));
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final int value = (Integer) newValue;
final MaterialColor selectedColor = MaterialColors.CONVERSATION_PALETTE.getByColor(getActivity(), value);
final MaterialColor currentColor = recipients.getColor();
if (selectedColor == null) return true;
if (preference.isEnabled() && !currentColor.equals(selectedColor)) {
recipients.setColor(selectedColor);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
DatabaseFactory.getRecipientPreferenceDatabase(getActivity())
.setColor(recipients, selectedColor);
return null;
}
}.execute();
}
return true;
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
PreferenceScreen prefScreen = getPreferenceScreen();
int count = prefScreen.getPreferenceCount();
for (int i = 0; i < count; i++) {
Preference p = prefScreen.getPreference(i);
if (!(p instanceof CheckBoxPreference)) {
String value = sharedPreferences.getString(p.getKey(), "");
setPreferenceSummary(p, value);
}
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Activity activity = getActivity();
if (key.equals(getString(R.string.pref_location_key))) {
// we've changed the location
// Wipe out any potential PlacePicker latlng values so that we can use this text entry.
SunshinePreferences.resetLocationCoordinates(activity);
SunshineSyncUtils.startImmediateSync(activity);
} else if (key.equals(getString(R.string.pref_units_key))) {
// units have changed. update lists of weather entries accordingly
activity.getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
}
Preference preference = findPreference(key);
if (null != preference) {
if (!(preference instanceof CheckBoxPreference)) {
setPreferenceSummary(preference, sharedPreferences.getString(key, ""));
}
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Activity activity = getActivity();
if (key.equals(getString(R.string.pref_location_key))) {
// we've changed the location
// Wipe out any potential PlacePicker latlng values so that we can use this text entry.
SunshinePreferences.resetLocationCoordinates(activity);
SunshineSyncUtils.startImmediateSync(activity);
} else if (key.equals(getString(R.string.pref_units_key))) {
// units have changed. update lists of weather entries accordingly
activity.getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
}
Preference preference = findPreference(key);
if (null != preference) {
if (!(preference instanceof CheckBoxPreference)) {
setPreferenceSummary(preference, sharedPreferences.getString(key, ""));
}
}
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
PreferenceScreen prefScreen = getPreferenceScreen();
int count = prefScreen.getPreferenceCount();
for (int i = 0; i < count; i++) {
Preference p = prefScreen.getPreference(i);
if (!(p instanceof CheckBoxPreference)) {
String value = sharedPreferences.getString(p.getKey(), "");
setPreferenceSummary(p, value);
}
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Activity activity = getActivity();
if (key.equals(getString(R.string.pref_location_key))) {
// we've changed the location
// Wipe out any potential PlacePicker latlng values so that we can use this text entry.
SunshinePreferences.resetLocationCoordinates(activity);
} else if (key.equals(getString(R.string.pref_units_key))) {
// units have changed. update lists of weather entries accordingly
activity.getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
}
Preference preference = findPreference(key);
if (null != preference) {
if (!(preference instanceof CheckBoxPreference)) {
setPreferenceSummary(preference, sharedPreferences.getString(key, ""));
}
}
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
PreferenceScreen prefScreen = getPreferenceScreen();
int count = prefScreen.getPreferenceCount();
for (int i = 0; i < count; i++) {
Preference p = prefScreen.getPreference(i);
if (!(p instanceof CheckBoxPreference)) {
String value = sharedPreferences.getString(p.getKey(), "");
setPreferenceSummary(p, value);
}
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}
private void setPreferenceSummary(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
}