gpt4 book ai didi

java - 为什么自定义的ExpandableListView在android中不显示

转载 作者:行者123 更新时间:2023-11-30 02:35:33 25 4
gpt4 key购买 nike

我正在尝试制作具有子节点或节点的自定义 ExpandableListView。但是有些 parent 没有 child 。我不想展示任何东西。也就是说

我需要这样显示

Parent 1
parent 2
child1
child2
child3
parent 3
parent 4
child1

所以我尝试像这样制作一个 xml route_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>

自定义适配器

public class CustomExpendableListView extends BaseExpandableListAdapter {
private String[] fatherName={"naveen","ravi","sharma","uncle","rahat"};
String[] raviChildrenname={"abc","pqr","mnn"};
String[] sharmaChildrenname={"zxa","yh","er"};
Context context;
public CustomExpendableListView(Context context) {
this.context=context;
}

@Override
public int getGroupCount() {
return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
return 0;
}

@Override
public Object getGroup(int groupPosition) {
return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}

@Override
public long getGroupId(int groupPosition) {
return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.parenttextview, null);
}
TextView item = (TextView) convertView.findViewById(R.id.parentTextView);
item.setText(fatherName[groupPosition]);
return item;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return null;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}

}

调用主函数

@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" >

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

parenttextviewxml

<?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>

我取三个数组

private String[] fatherName={"naveen","ravi","sharma","uncle","rahat"};
String[] raviChildrenname={"abc","pqr","mnn"};
String[] sharmaChildrenname={"zxa","yh","er"};

fatherName 是父节点。raviChildrennameravi 父节点的子节点。sharmaChildrenname 是子节点夏尔马

你能给一些代码吗?

最佳答案

您的 getGroupCount 方法必须返回大于零的值。在这种情况下,只有 ListView 会显示一些东西类似的东西:

@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;
}

关于java - 为什么自定义的ExpandableListView在android中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com