我有一个扩展ArrayAdapter的内部类,以自定义ListView。我想将此内部类分解为一个单独的文件,以便其他类可以使用它,但在getLayoutInflater()上遇到了一些麻烦。

基本上,即使扩展了ArrayAdapter,我的getView()方法也不知道getLayoutInflater()是什么。任何人都知道如何使它正常工作吗?

谢谢!

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();

10-05 18:44