Code example for Context

Methods: getResources

0
        } 
    } 
 
    public static ArrayAdapter<CharSequence> createFromResource(
            Context context, int textArrayResId, int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(
                textArrayResId);
        return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
    } 
 
    private boolean mAutoSetNotifyFlag = true;
    private Context mContext;
    private int mDropDownResource;
    private int mFieldId = 0;
    private ArrayFilter mFilter;
    private LayoutInflater mInflater;
    private final Object mLock = new Object();
    private boolean mNotifyOnChange = true;
    private List<T> mObjects;
    private ArrayList<T> mOriginalValues;
    private int mResource;
 
    public ArrayAdapter(Context context, int textViewResourceId) {
        init(context, textViewResourceId, 0, new ArrayList<T>());
    } 
 
    public ArrayAdapter(Context context, int resource, int textViewResourceId) {
        init(context, resource, textViewResourceId, new ArrayList<T>());
    } 
 
    public ArrayAdapter(Context context, int resource, int textViewResourceId,
                        List<T> objects) {
        init(context, resource, textViewResourceId, objects);
    } 
 
    public ArrayAdapter(Context context, int resource, int textViewResourceId,
                        T[] objects) {
        init(context, resource, textViewResourceId, Arrays.asList(objects));
    } 
 
    public ArrayAdapter(Context context, int textViewResourceId, List<T> objects) {
        init(context, textViewResourceId, 0, objects);
    } 
 
    public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
        init(context, textViewResourceId, 0, Arrays.asList(objects));
    } 
 
    public void add(T object) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.add(object);
            } else { 
                mObjects.add(object);
            } 
        } 
        if (mNotifyOnChange) {
            notifyDataSetChanged(); 
        } 
    } 
 
    public void addAll(Collection<? extends T> collection) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.addAll(collection);
            } else { 
                mObjects.addAll(collection);
            } 
        } 
        if (mNotifyOnChange) {
            notifyDataSetChanged(); 
        } 
    } 
 
    public void addAll(T... items) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                Collections.addAll(mOriginalValues, items);
            } else { 
                Collections.addAll(mObjects, items);
            } 
        } 
        if (mNotifyOnChange) {
            notifyDataSetChanged(); 
        } 
    } 
 
    public void clear() { 
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.clear();
            } else { 
                mObjects.clear();
            } 
        } 
        if (mNotifyOnChange) {
            notifyDataSetChanged(); 
        } 
    } 
 
    private View createViewFromResource(int position, View convertView,
                                        ViewGroup parent, int resource) {
        View view;
        TextView text = null;
        if (convertView == null) {
            view = mInflater.inflate(resource, parent, false);
        } else { 
            view = convertView;
        } 
        try { 
            if (view != null) {
                if (mFieldId > 0) {
                    text = (TextView) view.findViewById(mFieldId);
                } else { 
                    text = (TextView) view.findViewById(android.R.id.text1);
                } 
                if (text == null && view instanceof TextView) {
                    text = (TextView) view;
                } 
            } 
            if (text == null) {
                throw new NullPointerException();
            } 
        } catch (RuntimeException e) {
            Log.e("ArrayAdapter",
                    "You must supply a resource ID for a TextView"); 
            throw new IllegalStateException(
                    "ArrayAdapter requires the resource ID to be a TextView", e);
        } 
        T item = getItem(position);
        if (item instanceof CharSequence) {
            text.setText((CharSequence) item);
        } else { 
            text.setText(item.toString());
        } 
        return view;
    } 
 
    public Context getContext() {
        return mContext;
    } 
 
    @Override 
    public int getCount() { 
        return mObjects.size();
    } 
 
    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent,
                mDropDownResource);
    } 
 
    @Override 
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        } 
        return mFilter;
    } 
 
    @Override 
    public T getItem(int position) {
        return mObjects.get(position);
    } 
 
    @Override 
    public long getItemId(int position) {
        return position;
    } 
 
    public int getPosition(T item) {
        return mObjects.indexOf(item);
    } 
 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    } 
 
    private void init(Context context, int resource, int textViewResourceId,
                      List<T> objects) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mResource = mDropDownResource = resource;
        mObjects = objects;
        mFieldId = textViewResourceId;
    }