Code example for TypedArray

Methods: getText, hasValue

0
            return parseTextArray(a, index);
        } 
 
        @Override 
        public CharSequence getText(TypedArray a, int index) {
            return a.getText(index);
        } 
 
        @Override 
        public int getInt(TypedArray a, int index, int defaultValue) {
            return a.getInt(index, defaultValue);
        } 
 
        @Override 
        public int getFlag(TypedArray a, int index, int defaultValue) {
            return a.getInt(index, defaultValue);
        } 
 
        @Override 
        public boolean getBoolean(TypedArray a, int index, boolean defaultValue) {
            return a.getBoolean(index, defaultValue);
        } 
 
        protected static CharSequence[] parseTextArray(TypedArray a, int index) {
            if (!a.hasValue(index))
                return null; 
            final CharSequence text = a.getText(index);
            return parseCsvText(text);
        } 
 
        /* package */ static CharSequence[] parseCsvText(CharSequence text) {
            final int size = text.length();
            if (size == 0) return null;
            if (size == 1) return new CharSequence[] { text };
            final StringBuilder sb = new StringBuilder();
            ArrayList<CharSequence> list = null;
            int start = 0;
            for (int pos = 0; pos < size; pos++) {
                final char c = text.charAt(pos);
                if (c == ',') {
                    if (list == null) list = new ArrayList<CharSequence>();
                    if (sb.length() == 0) {
                        list.add(text.subSequence(start, pos));
                    } else { 
                        list.add(sb.toString());
                        sb.setLength(0);
                    } 
                    start = pos + 1;
                    continue; 
                } else if (c == '\\') {
                    if (start == pos) {
                        // Skip escape character at the beginning of the value. 
                        start++;
                        pos++;
                    } else { 
                        if (start < pos && sb.length() == 0)
                            sb.append(text.subSequence(start, pos));
                        pos++;
                        if (pos < size)
                            sb.append(text.charAt(pos));
                    } 
                } else if (sb.length() > 0) {
                    sb.append(c);
                } 
            } 
            if (list == null) {
                return new CharSequence[] { sb.length() > 0 ? sb : text.subSequence(start, size) };
            } else { 
                list.add(sb.length() > 0 ? sb : text.subSequence(start, size));
                return list.toArray(new CharSequence[list.size()]);
            } 
        } 
    } 
 
    private static class DeclaredKeyStyle extends EmptyKeyStyle { 
        private final HashMap<Integer, Object> mAttributes = new HashMap<Integer, Object>();
 
        @Override 
        public CharSequence[] getTextArray(TypedArray a, int index) {
            return a.hasValue(index)
                    ? super.getTextArray(a, index) : (CharSequence[])mAttributes.get(index);
        } 
 
        @Override 
        public CharSequence getText(TypedArray a, int index) {
            return a.hasValue(index)
                    ? super.getText(a, index) : (CharSequence)mAttributes.get(index);
        } 
 
        @Override 
        public int getInt(TypedArray a, int index, int defaultValue) {