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;
}
}
}
modified the code as below -
PS - NidhiGondhia requested for modified code, as in the comments this can not be fit, updating the modified code here.
You can do it more easily.
Steps:
Extend ListPreference
Override onPrepareDialogBuilder and replace mBuilder in DialogPreference with ProxyBuilder:
Handle getView in ProxyBuilder->AlertDialog->onShow->getListView->Adapter
This worked well for me. I used an Adapter approach that injects a wrapped adapter into the view.
Here is the base wrapped adapter class:
Here is the CustomListPreference base class that uses the ListPrefWrapperAdapter:
Finally, here are the derived classes that do the disabling and enabling of specific rows:
I have only tested this code on SDK version 15 and above.
OK I got this to work, mostly. I had to use a custom defined class that extends
ListPreference
. Then inside of that I had to create a custom adapter class just like you would for aListView
and set it to the builder usingbuilder.setAdapter()
. I also had to define listeners for both theRadioButtons
and theListView
rows that handled unchecking of theRadioButtons
and such. The only issues I still have are, my customListPreference
has both an OK and a Cancel button where aListPreference
only has the cancel button. I don't know how to remove the OK button. Also, I can't get the rows to highlight when I click on them like they do in a regularListPreference
.The java code for the custom
ListPreference
class. Be sure to mind things like your package name, the preference name (key), your entries and values for theListPreference
, and the names of your xml items.The xml for my
PreferenceActivity
. This is not my full xml, took out all my other preference items for simplicity. Again, be sure to mind the package name, the customListPreference
class must be referenced by the package name. Also mind the names of the preference and the array names that hold the entries and values.My xml for the dialog's list view row. In the getView method be sure to use the name of this xml file in the line that inflates this.
Finally, under res/values here is my array.xml that contains the entry names and values for the
ListPreference
. Again, shortened mine for simplicity.function
getcount()
returns is wrong.Probably have to add
editor.commit()
; after eacheditor.putInt(...)