Im having a customized ArrayAdapter for a spinner in my app. Here's the code for its getDropDownView() method:
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View vista = convertView;
if (vista==null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vista = inflater.inflate(R.layout.row_spinner,null);
}
TextView tv = (TextView) vista.findViewById( R.id.textview_entry );
if( !Utils.isSDKAbove( Utils.HONEY_COMB ) )
{
tv.setTextColor( getContext().getResources().getColor( android.R.color.primary_text_light ) );
}
tv.setText( getItem( position ) );
return vista;
}
and when tv.setText(), it throws the NullPointerException for TextView.
However , when I changed the
vista = inflater.inflate(R.layout.row_spinner, null);
to
vista = inflater.inflate(R.layout.row_spinner, parent, false);
it works.
can someone explain a bit more about the difference between the two different signature of the methods?
By declaring a parent root view, you are supplying a parent xml layout for that view. The third boolean parameter then determines whether or not this child view is attached to the parent view. Thereby determining whether child inherits the parent view's touch methods or not.
Either way the view needs to be put into perspective in terms of xml layout, so that the customisations and xml structure you've made will be implemented throughout your view hierarchy.
Using inflate
(layout, parent, false)You are using the parent layout to inflate the view (in this case spinner) without attaching it to the parent view. Using null you are not giving the view any layout parameters so the layout parameters for the xml for the textview doesn't exist.From the docs:
Using null is not good way to detach a view from the parent view, unless it is a stand alone feature, like an alert dialog.
The view needs a root view, passing null works some of the time, but only because the program attempts to create default xml parameters for the view.
This article goes into more detail.