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;
}
}
}
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.
I think you can achieve exactly what you want by setting the enabled flag of the ListPreference to false:
This grays out the description and makes it not selectable.
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:
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):
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!