user2495899
user2495899

Reputation:

ListView Inflater in BaseAdapter getView() method

I am using a custom adapter (BaseAdapter) for a ListView to show a list of objects in my program. I have implemented everything according to a tutorial I found online (http://www.codelearn.org/android-tutorial/android-listview), but I am getting an error when trying to setup the LayoutInflater in the getView() method of my Adapter class.

public View getView(int arg0, View arg1, ViewGroup arg2) {
    if(arg1==null) {

       LayoutInflater inflater =(LayoutInflater)EventListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       arg1 = inflater.inflate(R.layout.eventitem, arg2, false);
    }      

Where EventListActivity is my "MainActivity" where I am:

public class EventListActivity extends Activity {

private ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_list);
    //setting adapter to event list view
    EventBaseAdapter eventadapter = new EventBaseAdapter();
    listview = (ListView)findViewById(R.id.eventlist);
    listview.setAdapter(eventadapter);


}

I am getting an error at the line in the Adapter class where I am creating the LayoutInflater, at the part "EventListActivity.this", saying "No enclosing instance of the type EventListActivity is accessible in scope". I have no idea what this could mean and I would gladly take any advice or help given from you guys.

Thanks in Advance!

Upvotes: 0

Views: 1787

Answers (2)

Mushahid Khatri
Mushahid Khatri

Reputation: 728

Create LayoutInflater global object so that you can use it in other custom adapter also and user Inflater.inflat method on the getView() Method.

Like this

 public class EventListActivity extends Activity {

 private ListView listview;
 LayoutInflater inflater;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.event_list);

 //init inflater
 inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 //setting adapter to event list view
 EventBaseAdapter eventadapter = new EventBaseAdapter();
 listview = (ListView)findViewById(R.id.eventlist);
 listview.setAdapter(eventadapter);


}

And use it in your code..

public View getView(int arg0, View arg1, ViewGroup arg2) {
if(arg1==null) {


   arg1 = inflater.inflate(R.layout.eventitem, arg2, false);
}  

Upvotes: 0

Darpan
Darpan

Reputation: 5795

Create a constructor like this -

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
        this.activity = activity;
        ...
    }

And, use -

inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

This is because your Adapter file does not have any reference of your activity; Hence it can't fetch the layout inflater service. We will pass Activity's instance to its constructor and use it in CustomListAdapter.

Upvotes: 1

Related Questions