Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在自定义ListPreference中未更改所选项目_Java_Android - Fatal编程技术网

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

Java 在自定义ListPreference中未更改所选项目,java,android,Java,Android,似乎每次单击首选项时都会调用setChecked,并且所选项目会发生更改,但在对话框中,所选项目由于某种原因保持不变。我不知道发生了什么事。重新启动应用程序可能会更改所选项目,但这不是预期的行为:~ 下面是课程: public class IconListPreference extends ListPreference { private Context mContext; private LayoutInflater mInflater; private Drawab

似乎每次单击首选项时都会调用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;
        }
    }
}

编辑:用错误修正更新了我的代码,但仍然无法正常工作。找到了一个临时解决方案:在OnPreferenceChangeListener.onPreferenceChange.onPreferenceChange中调用setValueString newValue。

好,我终于得到了它。您需要执行以下操作才能使自定义首选项正常工作:

在设置用户提供的值之前调用ChangeListener; 如果需要更新用户界面,即当值更改时,notifyChanged。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="IconPreference">
        <attr name="entryIcons" format="reference" />
    </declare-styleable>
</resources>