5

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;

    }
}
wufoo
  • 13,571
  • 12
  • 53
  • 78

2 Answers2

14

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

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • Yeah that's what I thought too but getLayoutInflater() doesn't seem to be child method for the context in myDynAdap() nor is it a method for 'ViewGroup parent' in getView(). – wufoo Aug 05 '11 at 15:08
  • 1
    Well, you'll probably have to cast it to `Activity` to use `getLayoutInflater()` – jondavidjohn Aug 05 '11 at 15:11
  • Thanks for the response (upvoted). That's the solution I implemented. – wufoo Aug 05 '11 at 15:39
  • Why has the default arrayadapter not a Activity as a parameter but a Context baseclass there must be a reason for that. Is this not a bad practice to just cast it? – Roel Feb 05 '16 at 15:28
11

I will put the edits as comments:

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;

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • Hey, Thanks! I think that might work! I haven't worked the edits in yet (I simplified my example for brevity) but am doing so now. – wufoo Aug 05 '11 at 15:22
  • One question regarding your edits: I read that holding onto a Context is a way to cause memory leaks (I was playing with it as you caught in my code paste -- mCtx). Can you explain why this isn't a problem in the above code? I'm obviously missing something. I've seen other examples do this so wondering (eg: http://tinyurl.com/3cpvxsz )? – wufoo Aug 05 '11 at 15:29
  • Sherif - just implemented all the edits, works great! Really helps me keep things modular. Thanks! – wufoo Aug 05 '11 at 15:38
  • Sorry it is weekend :D .. For memory leak issue: Check http://stackoverflow.com/questions/3346080/android-references-to-a-context-and-memory-leaks – Sherif elKhatib Aug 06 '11 at 15:46
  • In general, holding onto contexts is bad. However, you can do things like create the LayoutInflater in the constructor, or swipe a context from the parent ViewGroup (parent.getContext()) – Joe Plante Sep 16 '13 at 18:12
  • Creating a LayoutInflater in the context is the same as holding a context. There is no danger in holding your context in the adapter. Except if you intend to do something really stupid with this adapter. – Sherif elKhatib Sep 16 '13 at 18:35