将适配器设置为v7.ListPreferences

时间:2016-03-26 20:13:09

标签: android android-preferences

我迁移到android.support.v7.preference,我对ListPreference项有一些问题。

我有那段代码:

public class CurrencyListPreferences extends ListPreference {
    private List<Currency> mCurrencyList;
    private Context mContext;

    public CurrencyListPreferences(Context context) {
        this(context, null);
    }

    public CurrencyListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public void setCurrencyList(List<Currency> currencyList) {
        this.mCurrencyList = currencyList;
    }

    protected void onPrepareDialogBuilder(Builder builder) {
        final ListAdapter listAdapter = (ListAdapter) new CurrencyAdapter(mContext, mCurrencyList, this);
        builder.setPositiveButton(null, null);
        builder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
    }

    public String getText() {
        return Preferences.getStringValue(mContext, TypePreference.TYPE_CURRENCYCODE);
    }
}

然后我有一个货币适配器,显示所需的不同值

当我迁移到android.support.v7.preference时,该代码不起作用。我有一条消息“ListPreference需要一个条目数组和一个entryValues数组。”

onPrepareDialogBu​​ilder不再被调用,有没有办法迁移我的旧代码?

如果没有解决方案,我想到了两个解决方案:

  • 迁移到setEntries和setEntriesValues(遗憾的是我会丢失布局)
  • 创建使用onPreferenceTreeClick处理的特定Dialog处理程序。听起来有点复杂......(并且难以维护)

此致

0 个答案:

没有答案