我可以在ListPreference中使用不同类型的ListView吗?

时间:2013-12-29 12:25:18

标签: android listpreference

我有一个自定义类型的ListView我试图用于ListPreference。我想扩展ListPreference的行为,但是放入我自己的自定义ListView,它包含的行为不是标准ListView类的一部分。这是我到目前为止的一些代码片段:

public class SortableListPreference extends ListPreference {
    public SortableListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        separator = DEFAULT_SEPARATOR;
        setPersistent(false);
        setDialogLayoutResource(R.layout.sort_list_array_dialog_preference);
    }

    @Override
    protected void onBindDialogView(View view)
    {
        super.onBindDialogView(view);
        ViewGroup group=(ViewGroup)view;
        //Update the view with the values of the preference
        SharedPreferences prefs = getSharedPreferences();
        mListView= (DragSortListView) view.findViewById(android.R.id.list);
        String value=prefs.getString(getKey(),null);
        CharSequence[] order=decodeValue(value,separator);
        if (order==null)
        {
            mAdapter =new ArrayAdapter<CharSequence>(mListView.getContext(),android.R.layout.simple_list_item_1);
        }
        else
        {
            mAdapter =new ArrayAdapter<CharSequence>(mListView.getContext(),android.R.layout.simple_list_item_1,order);
        }
        Log.v(TAG,"Setting adapter");
        mListView.setAdapter(mAdapter);
    }
    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        CharSequence[] entries = getEntries();
        CharSequence[] entryValues = getEntryValues();
        if (entries == null || entryValues == null
                || entries.length != entryValues.length) {
            throw new IllegalStateException(
                    "SortableListPreference requires an entries array and an entryValues "
                            + "array which are both the same length");
        }

        for (CharSequence entry:entries)
            mAdapter.add(entry);
            OnMultiChoiceClickListener listener = new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int which, boolean val) {
                entryChecked[which] = val;
            }
        };
        builder.setMultiChoiceItems(entries, entryChecked, listener);

    }
}

我还应该补充一点,我正在尝试通过API 9支持。当我这样做时,我看到的是:

enter image description here

请注意,列表会出现两次。底部是我要保留的清单,我不关心最重要的清单。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

为此,您应该拥有CustomAdater并覆盖getViewTypeCountgetViewItemType。 Android会通过你getViewTypeCount null convertView。

答案 1 :(得分:0)

密钥似乎没有使用构建器集侦听器或适配器设置。当我自己处理事件时,似乎工作正常。

protected void onPrepareDialogBuilder(Builder builder) {
    CharSequence[] entries = getEntries();
    CharSequence[] entryValues = getEntryValues();
    if (entries == null || entryValues == null
            || entries.length != entryValues.length) {
        throw new IllegalStateException(
                "SortableListPreference requires an entries array and an entryValues "
                        + "array which are both the same length");
    }

    for (CharSequence entry:entries)
        mAdapter.add(entry);
    mListView.setDropListener(new DropListener()
    {
        @Override
        public void drop(int from, int to) {

            CharSequence item = mAdapter.getItem(from);
            Log.v(TAG,"Moving item "+item+" from "+from+" to "+to);

            mAdapter.remove(item);
            mAdapter.insert(item, to);
            mAdapter.notifyDataSetChanged();
        }

    });
}