Question

I have a custom type of ListView I'm trying to use for a ListPreference. I would like to extend the behavior of ListPreference, but put in my own custom ListView, which incorporates behavior that isn't a part of the standard ListView class. Here's a few code snippets that I have so far:

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);

    }
}

I should also add that I'm trying to support through API 9. What I see when I do this is this:

enter image description here

Note that the list appears twice. The bottom is the list I want to keep, I don't care about the top one. Any thoughts?

Was it helpful?

Solution 2

The key seems to be not using the builder set listener or adapter settings. When I do my own handling of events, it seems to work just fine.

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();
        }

    });
}

OTHER TIPS

you should have a CustomAdater and override getViewTypeCount and getViewItemType, for this purpose. Android will pass you getViewTypeCount null convertView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top