Android 解析GetLayoutFlater时出错

Android 解析GetLayoutFlater时出错,android,Android,当我用下面的代码编译程序时,发生了一个错误。它说getLayoutInflater()未定义。谁能帮我尽快解决这个问题 final LayoutInflater inflater = getLayoutInflater ( ); 提前感谢。Afaik getLayoutInflater()是上下文提供的方法。如果此行位于一个不从上下文继承的类中,那么这将不起作用。向其传递一个上下文对象并调用此对象上的方法 编辑:对不起,我看了一下api文档,它是由HI Prasanth提供的,你可以试试这

当我用下面的代码编译程序时,发生了一个错误。它说getLayoutInflater()未定义。谁能帮我尽快解决这个问题

 final LayoutInflater inflater = getLayoutInflater ( ); 

提前感谢。

Afaik getLayoutInflater()是上下文提供的方法。如果此行位于一个不从上下文继承的类中,那么这将不起作用。向其传递一个上下文对象并调用此对象上的方法


编辑:对不起,我看了一下api文档,它是由

HI Prasanth提供的,你可以试试这个

步骤1:为布局充气器创建对象,如下所示:

LayoutInflater mInflater;
步骤2:通过传递上下文初始化

Context context=MyActivity.getApplicationContext();
mInflater = LayoutInflater.from(context);
步骤3:在get View meythod中,您可以将视图设置为

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


            if (convertView == null) {

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

这对我来说很有用: 在我的活动代码中,我将上下文作为参数传递给适配器,如下所示:

CustumAdapter myAdapter = new CustumAdapter(this, Items);
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);
然后我可以在适配器中使用它,如下所示:

CustumAdapter myAdapter = new CustumAdapter(this, Items);
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);
公共类CustumAdapter扩展BaseAdapter{
数组列表项;
公共语境;
公共CustumAdapter(上下文、ArrayList项){
这个。项目=项目;
this.context=上下文;
}
....
@凌驾
公共视图getView(int位置、视图转换视图、视图组父级){
LayoutInflater myInflater=LayoutInflater.from(上下文);
查看myview=myInflater.inflate(R.layout.row\u项,空);

一切正常:)

输入一些源代码和错误日志愚蠢的问题,但是。你忘了扩展
活动
类吗?另外,更多的代码也不错。尝试接受满意的答案。这将增加有人回答你未来问题的机会,这也将帮助整个社区。