Android: BaseAdapter and getLayoutInflater on separate class file Android: BaseAdapter and getLayoutInflater on separate class file android android

Android: BaseAdapter and getLayoutInflater on separate class file


There are more ways to get a LayoutInflater object than directly from an Activity. As a matter of fact, getLayoutInflater() is probably just a convenience method for this:

LayoutInflater li = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Please see the documentation for LayoutInflater.


you should pass the context of MainActivity when creating an object of the class that extends baseadaptersomething like thisLayoutInflater inflater = ((Activity)MyContext)).getLayoutInflater;

this would cast MyContext to an Activity and then GetLayoutInflater() could be called!!


Read this...

Application Fundamentals

...especially the bit on Activities in the Application Components section.

Do NOT try to instantiate an Activity using new. An Activity is a special-case Android class and should NOT be treated like a regular Java class. An Activity should only be started using an Intent and it's the Android OS's responsibility for instantiating it.

In other words, never do this...

Adapter MyGridAdapter = new Adapter();

Also, Adapter is the name of an Android widget class so not a good choice for the name of one of your own classes.

EDIT: Also see my answer to this question here about creating a helper class and passing the activity's Context to it.