【问题标题】:How to use a custom ArrayAdapter in a separate class?如何在单独的类中使用自定义 ArrayAdapter?
【发布时间】:2011-08-05 14:53:37
【问题描述】:

我有一个内部类,它扩展了 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;

    }
}

【问题讨论】:

    标签: android android-arrayadapter


    【解决方案1】:

    在传入的上下文中调用getLayoutInflater() 怎么样?

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

    【讨论】:

    • 是的,我也是这么想的,但 getLayoutInflater() 似乎不是 myDynAdap() 中上下文的子方法,也不是 getView() 中“ViewGroup parent”的方法。跨度>
    • 好吧,您可能必须将其转换为 Activity 才能使用 getLayoutInflater()
    • 感谢您的回复(赞成)。这就是我实施的解决方案。
    • 为什么默认的arrayadapter不是一个Activity作为参数而是一个Context基类,这一定是有原因的。只是施放它不是一个坏习惯吗?
    【解决方案2】:

    我会将编辑作为 cmets:

    public class myDynAdap extends ArrayAdapter<String>
    {
        String [] list;
        Context mContext; //ADD THIS to keep a context
    
       public myDynAdap (Context context, int textViewResourceId, String [] objects)
       {
           super (context, textViewResourceId, objects);
           mCtx = context; // remove this line! I don't think you need it
           this.mContext = context;
           list = objects;
       }
    
        @Override
        public View getView (int position, View convertView, ViewGroup parent)
        {
            View row = convertView;
    
            if (row == null)
            {
    
                LayoutInflater inflater = ((Activity)mContext).getLayoutInflater ();  // we get a reference to the activity
                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;
    
        }
    }
    

    【讨论】:

    • 嘿,谢谢!我认为这可能有效!我还没有进行编辑(为简洁起见,我简化了我的示例),但现在正在这样做。
    • 关于您的编辑的一个问题:我读到持有 Context 是导致内存泄漏的一种方式(我正在使用它,因为您在我的代码粘贴中遇到了 - mCtx)。你能解释一下为什么在上面的代码中这不是问题吗?我显然错过了一些东西。我见过其他例子这样做很奇怪(例如:tinyurl.com/3cpvxsz)?
    • Sherif - 刚刚实现了所有的编辑,效果很好!真的帮助我保持模块化。谢谢!
    • 抱歉是周末 :D .. 对于内存泄漏问题:检查stackoverflow.com/questions/3346080/…
    • 一般来说,坚持上下文是不好的。但是,您可以执行诸如在构造函数中创建 LayoutInflater 或从父 ViewGroup (parent.getContext()) 中滑动上下文等操作
    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多