滚动到ListPreference中的选定项目

时间:2013-01-21 18:41:54

标签: java android android-preferences

我有以下问题。 我创建了一个自定义列表首选项,用于显示带有附加图像的textview。 除了以下问题外,几乎所有工作都正常:

原始列表首选项滚动到当前选定的位置,但我的自定义列表首选项不显示此行为。如果我用android版本替换自定义列表首选项(或者在我的情况下是holoeverywhere版本),一切正常。 因此,我认为该bug必须在我当前的自定义列表首选项实现中。

public class CustomListPreference extends ListPreference {
private CustomListPreferenceAdapter customListPreferenceAdapter = null;
private Context mContext;
private LayoutInflater mInflater;
private CharSequence[] entries;
private CharSequence[] entryValues;
private Map<String, Boolean> rButtonMapping = null;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;

public CustomListPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    mInflater = LayoutInflater.from(context);
    rButtonMapping = new HashMap<String, Boolean>();
    prefs = PreferenceManager.getDefaultSharedPreferences(mContext);

    // init the button mapping with the default values
    for (int i = 0; i < Data.getData().length; i++) {
        rButtonMapping.put(Data.getData()[i], false);
    }

    // Set the current selected value to true
    String currentSelectedValue = prefs.getString(
            SettingsActivity.CUST_PREF_KEY, "TEST1");
    rButtonMapping.put(currentSelectedValue, true);

    editor = prefs.edit();
}

@Override
protected void onPrepareDialogBuilder(Builder builder) {
    entries = getEntries();
    entryValues = getEntryValues();

    if (entries == null || entryValues == null
            || entries.length != entryValues.length) {
        throw new IllegalStateException(
                "ListPreference requires an entries array and an entryValues array which are both the same length");
    }

    customListPreferenceAdapter = new CustomListPreferenceAdapter();

    builder.setAdapter(customListPreferenceAdapter,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
}

private class CustomListPreferenceAdapter extends BaseAdapter {

    public int getCount() {
        return entries.length;
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            row = mInflater.inflate(R.layout.custom_list_preference_row,
                    parent, false);
        }

        TextView text = (TextView) row
                .findViewById(R.id.settings_tv_test);
        text.setText(entries[position]);

        try {
            InputStream ims = mContext.getAssets().open(
                    "pics/" + entryValues[position] + ".png");
            BitmapDrawable d = new BitmapDrawable(mContext.getResources(),
                    ims);
            d.setTargetDensity(mContext.getResources().getDisplayMetrics().densityDpi);

            text.setCompoundDrawablesWithIntrinsicBounds(d, null, null,
                    null);
        } catch (IOException e) {
            text.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
                    null);
        }

        RadioButton rButton = (RadioButton) row
                .findViewById(R.id.settings_rb);
        // If the current position is checked we also check the current
        // value
        rButton.setChecked(rButtonMapping.get(entryValues[position]));
        row.setClickable(true);
        row.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for (String currentString : rButtonMapping.keySet()) {
                    rButtonMapping.put(currentString, false);
                }
                // Set the current selected value to true
                rButtonMapping.put((String) entryValues[position], true);

                // Also send the selected value to the editor
                editor.putString(SettingsActivity.CUST_PREF_KEY,
                        (String) entryValues[position]);
                editor.commit();

                getDialog().dismiss();
            }
        });

        rButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (String currentString : rButtonMapping.keySet()) {
                    rButtonMapping.put(currentString, false);
                }
                // Set the current selected value to true
                rButtonMapping.put((String) entryValues[position], true);

                // Also send the selected value to the editor
                editor.putString(SettingsActivity.CUST_PREF_KEY,
                        (String) entryValues[position]);
                editor.commit();

                getDialog().dismiss();
            }
        });

        return row;
    }
}

}

我也在这里研究了Stackoverflow和谷歌,但似乎没有人有这个问题。

有谁看到问题是什么?

2 个答案:

答案 0 :(得分:2)

我建议通过调用构建器的setSingleChoiceItems方法替换对setAdapter的调用:

builder.setSingleChoiceItems(adapter, adapter.getSelectedItem(), 
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            }
        });

adapter.getSelectedItem()将是一个返回当前所选行索引的方法。

答案 1 :(得分:0)

要滚动到ListPreference中的所选项目,只需在自定义setSelection课程的ListView方法中使用Android onCreateDialogView()的{​​{1}}方法。例如:

ListPreference