为什么自定义ExpandableListView没有显示在android中

时间:2014-10-29 07:34:45

标签: java android android-listview expandablelistview

我正在尝试使自定义ExpandableListView具有子级或节点。但是有些父母没有孩子。我不想展示任何东西。换句话说

我需要像那样显示

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 是父节点。 raviChildrenname ravi 父节点的子节点。 sharmaChildrenname 是孩子夏尔马

你可以请一些代码吗?

1 个答案:

答案 0 :(得分:1)

您的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;
}