如何使用自定义ArrayAdapter在一个单独的类? [英] How to use a custom ArrayAdapter in a separate class?

查看:134
本文介绍了如何使用自定义ArrayAdapter在一个单独的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,以自定义一个ListView扩展ArrayAdapter一个内部类。我想打破这个内部类出到一个单独的文件,因此其他类可以使用它,但具有getLayoutInflater一些麻烦()。

基本上,我getView()方法不知道是什么getLayoutInflater()是,即使我伸出ArrayAdapter。任何人都知道如何得到这个正常工作?

谢谢!

 公共类myDynAdap扩展ArrayAdapter<字符串>
{
    的String []列表;


   公共myDynAdap(上下文的背景下,INT textViewResourceId,字符串[]对象)
   {
       超(背景下,textViewResourceId,对象);
       mCtx =背景;
       名单=物体;
   }

    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        查看排= convertView;

        如果(行== NULL)
        {

            LayoutInflater充气= getLayoutInflater(); //< ---的方法getLayoutInflater()是未定义的类型myDynAdap
            行= inflater.inflate(R.layout.main_listitem,父母,假);
        }

        TextView的TV1 =(TextView中)row.findViewById(R.id.tv_item);
        tv1.setBackgroundColor(Color.BLUE);

        //改变第0列表元素的背景只有
        如果(位置== 0)
            tv1.setBackgroundColor(Color.CYAN);

        返回行;

    }
}
 

解决方案

什么叫 getLayoutInflater()上传递的上下文。

  LayoutInflater吹气=((活动)范围内).getLayoutInflater();
 

I have an inner class which extends ArrayAdapter in order to customize a ListView. I'd like to break this inner class out into a separate file so other classes can use it but having some trouble with getLayoutInflater().

Basically, my getView() method doesn't know what getLayoutInflater() is, even though I'm extending ArrayAdapter. Anyone know how to get this working correctly?

Thanks!

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;

    }
}

解决方案

What about calling getLayoutInflater() on the context that is passed in.

LayoutInflater inflater = ((Activity)context).getLayoutInflater();

这篇关于如何使用自定义ArrayAdapter在一个单独的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆