[英]Android adapter layout infalter error
I have an adapter, I want to inflate a view but I can't. 我有一个适配器,我想给视图充气,但不能。
This is my class code: 这是我的课程代码:
private static class RandomsAdapter extends BaseAdapter{
private Randoms randoms;
private RandomsAdapter(Randoms randoms) {
this.randoms=randoms;
}
public void updateRandoms(Randoms randoms) {
this.randoms=randoms;
notifyDataSetChanged();
}
@Override
public int getCount() {
return randoms.size();
}
@Override
public Random getItem(int position) {
return randoms.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position,View convertView,ViewGroup parent) {
View view=convertView;
if (convertView == null) {
convertView=getLayoutInflater().inflate(R.layout.random_bars,parent,false);
}
}
}
I am getting an error for this line: convertView=getLayoutInflater().inflate(R.layout.random_bars,parent,false);
我在此行遇到错误:
convertView=getLayoutInflater().inflate(R.layout.random_bars,parent,false);
Error: Cannot make a static reference to the non-static method getLayoutInflater() from the type Activity
错误:
Cannot make a static reference to the non-static method getLayoutInflater() from the type Activity
How can I resolve it? 我该如何解决?
you need Context inside this class& add it to constructor as 您需要在此类内部使用Context并将其添加到构造函数中
private RandomsAdapter(Randoms randoms, Context context) {
this.randoms=randoms;
this.mContext = context;
}
or remove "static" word from RandomsAdapter class definition 或从RandomsAdapter类定义中删除“静态”词
You need to pass Activity Context to your adapter. 您需要将活动上下文传递给适配器。 In the constructor:
在构造函数中:
private LayoutInflater inflater;
public RandomsAdapter(Context context, Randoms randoms) {
this.randoms=randoms;
this.inflater = LayoutInflater.from(context);
}
Then you inflate layout as: 然后将布局膨胀为:
convertView=inflater.inflate(R.layout.random_bars,parent,false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.