Siva Prakash
Siva Prakash

Reputation: 4997

ce.Apparently the method is not static

I am new to java and android.The method getContext() is directly called without referenced by an instance of View class. Apparently the method is not static. How is it possible to call a non static method directly.What am i missing here ? Have added my code below (the doubt is in last line). Thank you.

package in.shopperstreet.honeywell;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(Context context, String[] books) {
    super(context,R.layout.activity_main2,books);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater lif = LayoutInflater.from(getContext());
    ....

Upvotes: 0

Views: 26

Answers (2)

Farid
Farid

Reputation: 1096

the getContext method belongs to ArrayAdapter. from the documentation

Returns the context associated with this array adapter. The context is used to create views from the resource passed to the constructor.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007359

Use getLayoutInflater(), called on the activity hosting this adapter, over LayoutInflater.from().

public class CustomAdapter extends ArrayAdapter<String> {
    final private LayoutInflater li;

    public CustomAdapter(Activity host, String[] books) {
        super(host,R.layout.activity_main2,books);
        li=host.getLayoutInflater();
    }


    // other code goes here
}

Upvotes: 1

Related Questions