开发者

Error resolving getLayoutInflater (not defined)

While am co开发者_开发技巧mpiling the program with the below code, an error is occurring. It says getLayoutInflater ( ) is undefined. How can I resolve it?

 final LayoutInflater inflater = getLayoutInflater ( ); 


HI Prasanth, you can try this

Step 1: Create a object for Layout Inflater as shown here:

LayoutInflater mInflater;

Step2: Intialize by passing Context

Context context=MyActivity.getApplicationContext();
mInflater = LayoutInflater.from(context);

Step3: In get View meythod, you can intalize the view as

public View getView(int arg0, View myView, ViewGroup parent) {


            if (convertView == null) {

myView = mInflater.inflate(R.layout.mytest, null);
}


Afaik the getLayoutInflater() is a method provided by Context. If this line is located in a class which doesn't inherit from Context this won't work. Pass it a Context object and call the method on this object.

Edit: I'm sorry, had a look at the api docs, it's provided by Activity


This is what works for me : In my activity code, I pass the context as a parameter to my adapter like this :

CustumAdapter myAdapter = new CustumAdapter(this, Items);

then I can use it in my adapter like this :

public class CustumAdapter extends BaseAdapter {

ArrayList<ListItem> Items ;
public Context context;

public CustumAdapter(Context context, ArrayList<ListItem> Items) {
    this.Items = Items;
    this.context = context;
}
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {        
    LayoutInflater myInflater = LayoutInflater.from(context);
    View myview = myInflater.inflate(R.layout.row_item,null);

All works fine :)


LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜