在使用以下代码编译程序时,出现了一个错误。它说getLayoutInflater()未定义。我该如何解决?
final LayoutInflater inflater = getLayoutInflater ( );
在使用以下代码编译程序时,出现了一个错误。它说getLayoutInflater()未定义。我该如何解决?
final LayoutInflater inflater = getLayoutInflater ( );
嗨,Prasanth,你可以尝试这个方法:
步骤1:创建一个布局膨胀器对象,如下所示:
LayoutInflater mInflater;
第二步:通过传递上下文进行初始化
Context context=MyActivity.getApplicationContext();
mInflater = LayoutInflater.from(context);
第三步:在getView()方法中,您可以将视图初始化为
public View getView(int arg0, View myView, ViewGroup parent) {
if (convertView == null) {
myView = mInflater.inflate(R.layout.mytest, null);
}
据我所知,getLayoutInflater()是由Context提供的一个方法。如果这行代码位于不继承自Context的类中,则无法正常工作。传递一个Context对象并在该对象上调用该方法。
编辑:对不起,我看了api文档,它是由Activity提供的。
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);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Activity
类?此外,更多代码会更好。 - George