Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Custom ListViews Creating a Custom Adapter

fadhil suhendi
fadhil suhendi
7,680 Points

Context class

I don't really understand the use of the Context mContext in this class, can someone explain it?

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Fadhil

A Context object is used as a way of almost telling newly created objects what is going on with the current application/activity. You can pass the context (or current state information) along to new classes which you use in your current activity so they can reference information about the activity which created them.

The reason Context mContext is used is because the context will probably have been passed along using a constructor method and then set as a member variable for that object (naming convention for java is to used lowercase m followed by the variable name in camel case for member variables) so that it can be referenced in all methods of that class.

I'm sorry if that isn't all that clear, the important thing to remember is that if you create a new class which you intend to use in your activity and that new class will need access to a context object you can pass this along the chain as many times as necessary.

I also found this question on stack overflow if you wish to read further

http://stackoverflow.com/questions/3572463/what-is-context-in-android

Hope this helps Daniel