ION GROZEA
ION GROZEA

Reputation: 95

Custom list preference rendering issue

When I try to preview my preferences I get this error

Rendering Problems java.lang.UnsupportedOperationException   at android.content.res.BridgeResources.obtainTypedArray(BridgeResources.java:472)   at com.grozeaion.www.gvicameraremotecontrol.MyListPreference.(MyListPreference.java:52)   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)   at android.preference.GenericInflater.createItem(GenericInflater.java:385)   at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:432)   at android.preference.GenericInflater.rInflate(GenericInflater.java:483)   at android.preference.GenericInflater.rInflate(GenericInflater.java:495)   at android.preference.GenericInflater.inflate(GenericInflater.java:327)   at android.preference.Preference_Delegate.inflatePreference(Preference_Delegate.java:64) Copy stack to clipboard

I have created a custom list preference as follows

  1. attr.xml I have

    <declare-styleable name="MyListPreference">
    <attr name="android:id" />
    <attr name="android:key" />
    <attr name="android:entries" />
    <attr name="android:entryValues" />
    <attr name="android:defaultValue" />
    <attr name="itemIco" format="integer" />
    

  2. I have the control defined in preferences.xml

        <com.grozeaion.www.gvicameraremotecontrol.MyListPreference
        android:id="@+id/Dev2UseAs"
        android:icon="@drawable/off"
        android:key="Dev2UseAsK"
        android:summary="@string/trig_dev2m_s"
        android:title="@string/trig_dev2m_t"
        android:entries="@array/modeDevName"
        android:entryValues="@array/modeDevVal"
        android:defaultValue="0"
        custom:itemIco="@array/modeDevIco"
        />
    
  3. The array modeDevIco looks like this

    <array name="modeDevIco">
    <item>@drawable/off</item>
    <item>@drawable/camera</item>
    <item>@drawable/flash</item>
    <item>@drawable/split</item>
    

  4. In my custom class

    public MyListPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setLayoutResource(R.layout.my_list_value_main);
    this.context = context;
    Resources resources = context.getResources();
    preferences = PreferenceManager.getDefaultSharedPreferences(context);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyListPreference, 0, 0);
    try {
        crtCtrlKey = a.getString(R.styleable.MyListPreference_android_key);
        itemTitle = resources.getStringArray(a.getResourceId(R.styleable.MyListPreference_android_entries, 0));
        itemVal = resources.getStringArray(a.getResourceId(R.styleable.MyListPreference_android_entryValues, 0));
        defVal = a.getString(R.styleable.MyEditText_android_defaultValue);
        int len = itemVal.length;
        itemIco = new int[len];
        TypedArray iDs = resources.obtainTypedArray(a.getResourceId(R.styleable.MyListPreference_itemIco, 0));//get ID's for icons
        for (int i = 0; i < len; i++) {
            itemIco[i] = iDs.getResourceId(i,0);
        }
        iDs.recycle();
    } finally {
        a.recycle();
    }
    

    }

line 52 is

TypedArray iDs = resources.obtainTypedArray(a.getResourceId(R.styleable.MyListPreference_itemIco, 0));//get ID's for icons

How can i fix this rendering issue? i am guessing that is because of how i am getting the icons IDs but i don't know how to do it otherwise

Upvotes: 2

Views: 436

Answers (1)

GreatC
GreatC

Reputation: 400

I have the same rendering issue. But I am trying to get the color resources. I worked it out by isInEditMode().

private int[] getColorsById(int id){
    if(isInEditMode()){
        String[] s=mContext.getResources().getStringArray(id);
        int[] colors = new int[s.length];
        for (int j = 0; j < s.length; j++){
            colors[j] = Color.parseColor(s[j]);
        }
        return colors;
    }else{
        TypedArray typedArray=mContext.getResources().obtainTypedArray(id);
        int[] colors = new int[typedArray.length()];
        for (int j = 0; j < typedArray.length(); j++){
            colors[j] = typedArray.getColor(j,Color.BLACK);
        }
        typedArray.recycle();
        return colors;
    }
}

I hope it can help you a lot. Or you have a better way to figure it out. Just comment me.

Upvotes: 3

Related Questions