我有一个内部类,它扩展了 ArrayAdapter 以自定义 ListView。我想将这个内部类分解成一个单独的文件,以便其他类可以使用它,但在使用 getLayoutInflater() 时遇到了一些麻烦。
基本上,我的 getView() 方法不知道 getLayoutInflater() 是什么,即使我正在扩展 ArrayAdapter。任何人都知道如何让它正常工作?
谢谢!
public class myDynAdap extends ArrayAdapter<String>
{
String [] list;
public myDynAdap (Context context, int textViewResourceId, String [] objects)
{
super (context, textViewResourceId, objects);
mCtx = context;
list = objects;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = getLayoutInflater (); // <--- "The method getLayoutInflater() is undefined for the type myDynAdap"
row = inflater.inflate (R.layout.main_listitem, parent, false);
}
TextView tv1 = (TextView) row.findViewById (R.id.tv_item);
tv1.setBackgroundColor (Color.BLUE);
// change background of 0th list element only
if (position == 0)
tv1.setBackgroundColor (Color.CYAN);
return row;
}
}
最佳答案
如何在传入的上下文中调用 getLayoutInflater()
。
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
关于android - 如何在单独的类中使用自定义 ArrayAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6958279/