How to get context for BaseAdapter in Android

Sid picture Sid · May 16, 2013 · Viewed 12.6k times · Source

I have a class named BinderData which extends BaseAdapter. I want to get the base class context. I tried to use getContext() but that doesn't work in case of BaseAdapter.

How can I get the Context of this class?

Answer

My God picture My God · Feb 20, 2014

One more solution-

If you have a parent, you can directly access the context like so-

 public class SampleAdapter extends BaseAdapter {

            LayoutInflater inflater;

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(inflater == null){
                Context context = parent.getContext();
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }
                ...
                ...

                return convertView;
            }

    }