来自 listPreference中的同一个自定义行?我尝试创建自己的自定义列表首选项,但没有保存任何内容或没有填充旧索引。
public class FontSizePreference extends ListPreference {
private CustomListPreferenceAdapter customListPreferenceAdapter = null;
private Context mContext;
private LayoutInflater mInflater;
// private CharSequence[] entries;
// private CharSequence[] entryValues;
private ArrayList<RadioButton> rButtonList;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private static final String TAG = FontSizePreference.class.getSimpleName();
private String font_size = "";
public FontSizePreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mInflater = LayoutInflater.from(context);
rButtonList = new ArrayList<RadioButton>();
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
editor = prefs.edit();
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
font_size = prefs.getString(KEY_FONT_SIZE, "18");
if (getEntries() == null || getEntryValues() == null
|| getEntries().length != getEntryValues().length) {
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array which are both the same length");
}
customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext, getEntries(),
getEntryValues());
builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
super.onPrepareDialogBuilder(builder);
}
private class CustomListPreferenceAdapter extends BaseAdapter {
private CharSequence[] entries;
private CharSequence[] entryValues;
public CustomListPreferenceAdapter(Context mContext, CharSequence[] entries,
CharSequence[] entryValues) {
this.entries = entries;
this.entryValues = entryValues;
}
public int getCount() {
return entries.length;
}
public Object getItem(int position) {
return entries[position];
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
CustomHolder holder = null;
View row = convertView;
if (row == null) {
row = mInflater.inflate(R.layout.fontsize_layout, parent, false);
}
boolean flag = false;
for (CharSequence entry : entryValues) {
if (entry.toString().equals(font_size)) {
flag = true;
}
}
holder = new CustomHolder(row, position, font_size, flag);
row.setTag(holder);
row.setClickable(false);
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for (RadioButton rb : rButtonList) {
Log.d(TAG, "current row listener : " + rb.getId() + " : : : " + position);
if (rb.getId() != position)
rb.setChecked(false);
else
rb.setChecked(true);
}
int index = position;
String value = (String)entryValues[index];
editor.putString(KEY_FONT_SIZE, value).commit();
getDialog().dismiss();
}
});
return row;
}
class CustomHolder {
private TextView text = null;
private RadioButton rButton = null;
CustomHolder(final View row, final int position, String prefValue, boolean checked) {
Log.d(TAG, "current position is : " + position);
text = (TextView)row.findViewById(R.id.fontSizeTitle);
text.setText(entries[position]);
text.setTextSize(Float.valueOf(entryValues[position].toString()));
rButton = (RadioButton)row.findViewById(R.id.fontSizeState);
int newIndex = Arrays.asList(entryValues).indexOf(prefValue);
// rButton.setId(newIndex);
if (position != newIndex)
rButton.setChecked(false);
rButtonList.add(rButton);
rButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
for (RadioButton rb : rButtonList) {
if (rb != buttonView)
rb.setChecked(false);
else {
rb.setChecked(true);
}
}
}
int index = buttonView.getId();
String value = (String)entryValues[position];
editor.putString(KEY_FONT_SIZE, value).commit();
getDialog().dismiss();
}
});
}
}
}
每次我运行此示例时,它都会生成 35 个单选按钮实例,并且它不会在视图中显示先前选择的按钮或当前选择的按钮。请帮我解决这个问题。这是数组、布局和首选项调用的 xml 代码
<string-array name="font_size_options">
<item>Larger</item>
<item>Large</item>
<item>Medium</item>
<item>Small</item>
<item>Smallest</item>
</string-array>
<string-array name="font_size_values">
<item>26</item>
<item>20</item>
<item>18</item>
<item>14</item>
<item>10</item>
</string-array>
布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="@+id/fontSizeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
/>
<RadioButton
android:id="@+id/fontSizeState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0" />
</LinearLayout>
偏好是:
<com.example.samplepreference.FontSizePreference
android:key="semc_pref_key_font_size"
android:entries="@array/font_size_options"
android:entryValues="@array/font_size_values"
android:title="@string/font_size_title" />
设置按钮也(rButton.setId(newIndex))
总是返回空指针。