Skip to content

Commit 9e7f41e

Browse files
Romain GuyAndroid Code Review
authored andcommitted
Merge "Allow ListPreference summary to use entry"
2 parents c9c987d + ba636df commit 9e7f41e

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

core/java/android/preference/ListPreference.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ListPreference extends DialogPreference {
3939
private CharSequence[] mEntries;
4040
private CharSequence[] mEntryValues;
4141
private String mValue;
42+
private String mSummary;
4243
private int mClickedDialogEntryIndex;
4344

4445
public ListPreference(Context context, AttributeSet attrs) {
@@ -49,8 +50,16 @@ public ListPreference(Context context, AttributeSet attrs) {
4950
mEntries = a.getTextArray(com.android.internal.R.styleable.ListPreference_entries);
5051
mEntryValues = a.getTextArray(com.android.internal.R.styleable.ListPreference_entryValues);
5152
a.recycle();
53+
54+
/* Retrieve the Preference summary attribute since it's private
55+
* in the Preference class.
56+
*/
57+
a = context.obtainStyledAttributes(attrs,
58+
com.android.internal.R.styleable.Preference, 0, 0);
59+
mSummary = a.getString(com.android.internal.R.styleable.Preference_summary);
60+
a.recycle();
5261
}
53-
62+
5463
public ListPreference(Context context) {
5564
this(context, null);
5665
}
@@ -126,6 +135,43 @@ public void setValue(String value) {
126135
persistString(value);
127136
}
128137

138+
/**
139+
* Returns the summary of this ListPreference. If the summary
140+
* has a {@linkplain java.lang.String#format String formatting}
141+
* marker in it (i.e. "%s" or "%1$s"), then the current entry
142+
* value will be substituted in its place.
143+
*
144+
* @return the summary with appropriate string substitution
145+
*/
146+
@Override
147+
public CharSequence getSummary() {
148+
final CharSequence entry = getEntry();
149+
if (mSummary == null || entry == null) {
150+
return super.getSummary();
151+
} else {
152+
return String.format(mSummary, entry);
153+
}
154+
}
155+
156+
/**
157+
* Sets the summary for this Preference with a CharSequence.
158+
* If the summary has a
159+
* {@linkplain java.lang.String#format String formatting}
160+
* marker in it (i.e. "%s" or "%1$s"), then the current entry
161+
* value will be substituted in its place when it's retrieved.
162+
*
163+
* @param summary The summary for the preference.
164+
*/
165+
@Override
166+
public void setSummary(CharSequence summary) {
167+
super.setSummary(summary);
168+
if (summary == null && mSummary != null) {
169+
mSummary = null;
170+
} else if (summary != null && !summary.equals(mSummary)) {
171+
mSummary = summary.toString();
172+
}
173+
}
174+
129175
/**
130176
* Sets the value to the given index from the entry values.
131177
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2009 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.preference;
18+
19+
import android.preference.ListPreference;
20+
import android.test.AndroidTestCase;
21+
22+
public class ListPreferenceTest extends AndroidTestCase {
23+
public void testListPreferenceSummaryFromEntries() {
24+
String[] entries = { "one", "two", "three" };
25+
String[] entryValues = { "1" , "2", "3" };
26+
ListPreference lp = new ListPreference(getContext());
27+
lp.setEntries(entries);
28+
lp.setEntryValues(entryValues);
29+
30+
lp.setValue(entryValues[1]);
31+
assertTrue(lp.getSummary() == null);
32+
33+
lp.setSummary("%1$s");
34+
assertEquals(entries[1], lp.getSummary());
35+
36+
lp.setValue(entryValues[2]);
37+
assertEquals(entries[2], lp.getSummary());
38+
39+
lp.setSummary(null);
40+
assertTrue(lp.getSummary() == null);
41+
42+
lp.setSummary("The color is %1$s");
43+
assertEquals("The color is " + entries[2], lp.getSummary());
44+
}
45+
}

0 commit comments

Comments
 (0)