Java 为什么android中不显示自定义ExpandableListView
我正在尝试使自定义Java 为什么android中不显示自定义ExpandableListView,java,android,android-listview,expandablelistview,Java,Android,Android Listview,Expandablelistview,我正在尝试使自定义ExpandableListView具有子节点或节点。但是有些父母没有孩子。我不想展示任何东西。换句话说 我需要这样展示 Parent 1 parent 2 child1 child2 child3 parent 3 parent 4 child1 所以我试着这样做,我制作了一个xmlroute\u dashboard.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xml
ExpandableListView
具有子节点或节点。但是有些父母没有孩子。我不想展示任何东西。换句话说
我需要这样展示
Parent 1
parent 2
child1
child2
child3
parent 3
parent 4
child1
所以我试着这样做,我制作了一个xmlroute\u dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
>
</ExpandableListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/childTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/parentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
调用主功能
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.route_dashboard);
expendablelistView = (ExpandableListView) findViewById(R.id.expandableListView1);
adapter = new CustomExpendableListView(this);
expendablelistView.setAdapter(adapter);
}
child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
>
</ExpandableListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/childTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/parentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
fatherName是父节点。raviChildrenname是ravi父节点的子节点。sharmaChildrenname是sharma
您能给出一些代码吗?您的getGroupCount方法必须返回大于零的值。在这种情况下,只有列表视图将显示某些内容 诸如此类:
@Override
public int getGroupCount() {
return fatherName.length;
}
@Override
public int getChildrenCount(int groupPosition) {
if (fatherName[groupPosition].equalsIgnoreCase("ravi")) {
return raviChildrenname.length;
}
if (fatherName[groupPosition].equalsIgnoreCase("sharma")) {
return sharmaChildrenname.length;
}
return 0;
}
您还需要为此listView实现getChildView方法:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.childView, null);
}
TextView item = (TextView) convertView.findViewById(R.id.childTextView);
if (fatherName[groupPosition].equalsIgnoreCase("ravi")) {
item.setText(raviChildrenname[childPosition]);
}
if (fatherName[groupPosition].equalsIgnoreCase("sharma")) {
item.setText( sharmaChildrenname[childPosition]);
}
return item;
}
那么您的ExpListView根本没有显示?您在适配器中的返回语句始终为0或null。例如,在getChildCount或getGroupCount中,返回0,因此这将不起作用。此外,getChildView返回null,因此您不会在列表中获得任何子级。请参见此处,了解一个简单的ExpandableListView示例:还要维护一个映射父子关系的Hashmap,这样您就可以看到如果父对象没有子对象,则不需要显式返回0使用
Hashmap测试,并在get childrencount中返回测试中映射的特定字符串的数组列表大小Hashmap
此外,get child不应为null。请看@opiatefuchs提到的一个简单示例。请您更改一些代码……或者更改我的代码。如果您使用线性布局,请在列表中添加权重,或者将其设置为与@Sergey Pekar answer匹配