Reputation: 5406
Passing null for root studio gives me this warning:
Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)
It is showing a null value in getGroupView
. Please help.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
super();
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Upvotes: 278
Views: 107835
Reputation: 2290
Edit:
At the time of writing this answer, I was not aware of Lint suppressions. It's better to suppress Lint instead of tricking the IDE. Add this above the line or method:
@SuppressLint("InflateParams")
Old answer:
When you really don't have any parent
(for example creating view for AlertDialog
), you have no other way than passing null
. So do this to avoid warning:
final ViewGroup nullParent = null;
convertView = layoutInflater.inflate(R.layout.list_item, nullParent);
Upvotes: 38
Reputation: 6209
I found a good article about LayoutInflater
. The author explains how all the versions of the inflate
method work and gives examples of ListView
and AlertDialog
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
Update #1.
This answer recently helped me, too. https://stackoverflow.com/a/5027921/1065835
Upvotes: 62
Reputation: 21
Here is a picture for reference:
return inflater.inflate(R.layout.some_layout, container, true);
// for the last parameter use true, false or simply don't declare
Upvotes: 2
Reputation: 921
For AlertDialog
bellow code can be used
convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);
Upvotes: 12
Reputation: 1243
Here ya go, for some reason using View.inflate instead of inflating from a layoutinflater makes the lint error disappear. Thought I'd post this here since this thread is at the top of the Google Search...
view = View.inflate(context,R.layout.custom_layout,null);
Upvotes: 58
Reputation: 27237
A nice information I found while looking for a way to solve this.According to Dave Smith,
Layout inflation is the term used within the context of Android to indicate when an XML layout resource is parsed and converted into a hierarchy of View objects.
The ViewGroup
been asked for here as part of the inflate method parameters is used to inherit higher level styling.While passing null can seem harmless, it can actually cause serious problems for your app later on. Read more about this here.
Upvotes: 7
Reputation: 8866
Instead of doing
convertView = infalInflater.inflate(R.layout.list_item, null);
do
convertView = infalInflater.inflate(R.layout.list_item, parent, false);
It will inflate it with the given parent, but won't attach it to the parent.
Upvotes: 410