Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

android - 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;

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...