Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a custom ArrayAdapter in a separate class?

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;

    }
}
like image 995
wufoo Avatar asked Aug 05 '11 14:08

wufoo


People also ask

How do you populate a ListView?

To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.

How does an array adapter work?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


1 Answers

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

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
like image 175
jondavidjohn Avatar answered Sep 21 '22 03:09

jondavidjohn