我试图显示一个自定义列表。我正在将一个字符串数组传递给class customAdapter。当我尝试访问getLayoutInflater时无法识别?我该如何解决这个问题
package com.parse.starter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
ArrayList<String> descriptions;
public CustomAdapter(final ArrayList<String> description) {
descriptions=description;
}
@Override
public int getCount() {
return descriptions.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view=getLayotInflater(R.layout.rowcourses,null);
return null;
}
}
答案 0 :(得分:2)
在这里,更改此构造函数
private Context context;
public CustomAdapter(Context context, final ArrayList<String> description) {
descriptions=description;
context = context;
}
并对此进行修改
@Override
public View getView(int position, View view, ViewGroup parent) {
// so you would require an Inflater service which will load/inflate the layout, and to use service,
// you need to have access to context, which you can get it from the constructor by passing it from your host class.
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View aView=inflater.inflate(R.layout.rowcourses, parent,false);
// once you load the view, you need to return that, in your case you were returning null
return aView;
}