所选项目未在自定义ListPreference中更改

时间:2014-08-25 06:26:37

标签: java android

每次单击首选项并且所选项目发生更改时,似乎都会调用setChecked但在对话框中所选项目由于某种原因保持不变。我不知道发生了什么。重新启动应用程序可以使所选项目发生更改,但这不是预期的行为。 :〜

这是班级:

public class IconListPreference extends ListPreference {
    private Context mContext;
    private LayoutInflater mInflater;
    private Drawable[] mEntryIcons = null;
    private String mKey;
    private int selectedEntry = -1;

    public IconListPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconListPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0);
        int entryIconsResId = a.getResourceId(R.styleable.IconPreference_entryIcons, -1);
        if (entryIconsResId != -1) setEntryIcons(entryIconsResId);
        mInflater = LayoutInflater.from(context);
        mKey = getKey();
        a.recycle();
    }

    public void setEntryIcons(Drawable[] entryIcons) {
        mEntryIcons = entryIcons;
    }

    public void setEntryIcons(int entryIconsResId) {
        TypedArray icons_array = mContext.getResources().obtainTypedArray(entryIconsResId);
        Drawable[] icon_ids_array = new Drawable[icons_array.length()];
        for (int i = 0; i < icons_array.length(); i++) icon_ids_array[i] = icons_array.getDrawable(i);
        setEntryIcons(icon_ids_array);
        icons_array.recycle();
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        CharSequence[] entries = getEntries(), entryValues = getEntryValues();
        if (entries.length != entryValues.length) throw new IllegalStateException
                ("ListPreference requires an entries array and an entryValues array which are both the same length");
        if (mEntryIcons != null && entries.length != mEntryIcons.length) throw new IllegalStateException
                ("IconListPreference requires the icons entries array be the same length than entries or null");
        IconListPreferenceScreenAdapter iconListPreferenceAdapter = new IconListPreferenceScreenAdapter();
        if (mEntryIcons != null) {
            String selectedValue = getPreferenceManager().getSharedPreferences().getString(mKey, "");
            for (int i = 0; i < entryValues.length; i++) {
                if (selectedValue.compareTo((String) entryValues[i]) == 0) {
                    selectedEntry = i;
                    break;
                }
            }
            builder.setAdapter(iconListPreferenceAdapter, null);
        }
        super.onPrepareDialogBuilder(builder);
    }

    private class IconListPreferenceScreenAdapter extends BaseAdapter {
        public int getCount() {
            return mEntryIcons.length;
        }

        class CustomHolder {
            private CheckedTextView text = null;

            CustomHolder(View row, int position) {
                text = (CheckedTextView) row.findViewById(android.R.id.text1);
                text.setText(getEntries()[position]);
                text.setChecked(selectedEntry == position);
                if (mEntryIcons != null)
                    text.setCompoundDrawablesWithIntrinsicBounds(mEntryIcons[position], null, null, null);
            }
        }

        public Object getItem(int position) {
            return getEntries()[position];
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = mInflater.inflate(Resources.getSystem()
                            .getIdentifier("select_dialog_singlechoice_holo", "layout", "android"), parent, false);
            CustomHolder holder;
            final int p = position;
            holder = new CustomHolder(convertView, position);
            convertView.setTag(holder);
            convertView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    v.requestFocus();
                    getDialog().dismiss();
                    IconListPreference.this.callChangeListener(getEntryValues()[p]);
                    SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit();
                    editor.putString(mKey, getEntryValues()[p].toString());
                    selectedEntry = p;
                    editor.apply();
                }
            });
            return convertView;
        }
    }
}

res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="IconPreference">
        <attr name="entryIcons" format="reference" />
    </declare-styleable>
</resources>

编辑:用错误修正更新了我的代码,但仍然没有工作。找到临时解决方案:在setValue((String) newValue)中调用OnPreferenceChangeListener.onPreferenceChange

1 个答案:

答案 0 :(得分:0)

好的,我终于明白了。您需要执行以下操作才能使自定义首选项有效:

    在设置用户提供的值之前
  • callChangeListener;
  • notifyChanged如果您需要更新用户界面,即每当值发生变化时。