How to use a custom ArrayAdapter in a separate class?

后端 未结 2 758
太阳男子
太阳男子 2020-12-11 06:46

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

相关标签:
2条回答
  • 2020-12-11 07:17

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

    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    
    0 讨论(0)
  • 2020-12-11 07:21

    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;
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题