custom row in a listPreference?

2019-01-04 00:16发布

I am trying to create a ListPreference but somehow disable one of the items. Sort of like gray it out or something and not have the ability to choose it. It will be an upcoming feature and I want it to be in the list just not selectable.

I have created a custom ListPreference class and in that class a custom adapter, hoping to use the adapter to create what I want.

The code works, and it sets the adapter, but none of the adapter functions get called. I set breakpoints on the methods, such as getCount() but they never get called.

Here's my code. Custom ListPreference taken from http://blog.350nice.com/wp/archives/240

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.app.AlertDialog.Builder;

public class CustomListPreference extends ListPreference {

    private boolean[] mClickedDialogEntryIndices;
    CustomListPreferenceAdapter customListPreferenceAdapter = null;
    Context mContext;

    public CustomListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mClickedDialogEntryIndices = new boolean[getEntries().length];
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        CharSequence[] entries = getEntries();
        CharSequence[] 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");
        }
        builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices,
                new DialogInterface.OnMultiChoiceClickListener() {

                    public void onClick(DialogInterface dialog, int which,
                            boolean val) {
                        mClickedDialogEntryIndices[which] = val;
                    }
                });
        // setting my custom list adapter
        customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);
        builder.setAdapter(customListPreferenceAdapter, null);
    }

    private class CustomListPreferenceAdapter extends BaseAdapter {

        public CustomListPreferenceAdapter(Context context) {}

        public int getCount() {
            return 1;
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            convertView.setBackgroundColor(Color.BLUE);
            return convertView;
        }
    }
}

9条回答
仙女界的扛把子
2楼-- · 2019-01-04 00:58

Thanks Bob for that answer, and Vamsi for trying to correct the duplicate entries bug, but Vamsi's fix didn't work for me. I had to keep an array of views and return it on the position if it had already been created before. So here is my full CustomListPreferenceAdapter class. It also contains the fix to check the selected preference value.

private class CustomListPreferenceAdapter extends BaseAdapter
{
    View[] Views;

    public CustomListPreferenceAdapter(Context context)
    {
        Views = new View[entries.length];
    }

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

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

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

    public View getView(final int position, View convertView, ViewGroup parent)
    {  
        View row = Views[position];
        CustomHolder holder = null;

        if(row == null)
        {                                                             
            row = mInflater.inflate(R.layout.listrow, parent, false);
            holder = new CustomHolder(row, position);
            row.setTag(holder);
            Views[position] = row;
        }

        return row;
    }

    class CustomHolder
    {
        private TextView text = null;
        private RadioButton rButton = null;

        CustomHolder(View row, int position)
        {    
            text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view);
            text.setText(entries[position]);

            rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
            rButton.setId(position);

            if(getPersistedString("").compareTo((String)entryValues[position])==0)
                rButton.setChecked(true);

            rButtonList.add(rButton);
        }
    }
}
查看更多
叛逆
3楼-- · 2019-01-04 00:58

I think you can achieve exactly what you want by setting the enabled flag of the ListPreference to false:

ListPreference lp = (ListPreference) findPreference("YOUR_KEY");
lp.setEnabled(false);

This grays out the description and makes it not selectable.

查看更多
ら.Afraid
4楼-- · 2019-01-04 01:05

This worked for me, but it did not work well if the list does not fit on the screen (and requires scrolling). It took me a loooong time to find the solution (but I finally did).

First the problem: As described here: getView called with wrong position when scrolling fast you will get unpredictable behavior when you use an onclick listener in:

public View getView(final int position, View convertView, ViewGroup parent)

In my case, the onClick event would be stored in memory and would be executed when the user tried to scroll (slightly).

And now the solution: Put the onClick listener in the main class (at least this worked for me):

public class CustomListPreference extends ListPreference {

// Other code (see above)
@Override
protected void onPrepareDialogBuilder(Builder builder)
{
    builder.setPositiveButton(null, null);

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

    builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int position)
        {
            // Code here, using position to indicate the row that was clicked...
            dialog.dismiss();
        }
    });

}

Spend waaaay too much time on this, so hope it will help someone out :)

All in all, still really happy with this code example! (use it as a color picker).

P.S. If you like this post, please vote useful. Thx!

查看更多
登录 后发表回答