ExpandableListView不显示任何数据

时间:2013-11-10 07:57:05

标签: android expandablelistview

我已经实现了一个自定义的ExpandableListView,但它没有显示任何数据。

这是我的布局。

activity_cust_mainlist.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/tv_cust_salesRep"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="94dp"
        android:gravity="left"
        android:text="sales man"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tv_cust_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_cust_salesRep"
        android:gravity="center"
        android:text="Lista de Clientes"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_cust_header"
        android:layout_centerHorizontal="true"
        android:weightSum="11" >

        <TextView
            android:id="@+id/tv_custtime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:text="Horario"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/tv_custid"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Cod"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/tv_custname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:gravity="center"
            android:text="Nombre"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <ExpandableListView
        android:id="@+id/eLV_customers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lin" >
    </ExpandableListView>

</RelativeLayout>

activity_cust_group_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:weightSum="11">

    <TextView
        android:id="@+id/tv_custList_Time"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:gravity="center"
        android:text="Horario"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tv_custList_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:text="Cod"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tv_custList_Name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:gravity="center"
        android:text="Nombre"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

activity_cust_child_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_clildList_refe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="REFE"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

实际代码:CustomerActivity

  package com.dhiraj.mpos;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.TextView;

    public class CustomerActivity extends Activity {

        private ArrayList<String> parentItems;
        private ArrayList<Object> childItems;

        ExpandableListView listView;
        ExpandableListAdapter listAdapter;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cust_temp);

            parentItems = new ArrayList<String>();
            childItems = new ArrayList<Object>();
            setGroupParents();
            setChildData();

            listView = (ExpandableListView) findViewById(R.id.eLV_customers);
            listAdapter = new MyCustomExpandableAdapter(parentItems, childItems);

            listView.setAdapter(listAdapter);
            Log.i("dhiraj", "adapter set");

        }

        private class MyCustomExpandableAdapter extends BaseExpandableListAdapter {
            ArrayList<String> parent,child;
            ArrayList<Object> children;

            MyCustomExpandableAdapter(ArrayList<String> parents,
                    ArrayList<Object> childern) {
                this.parent = parents;
                this.children = childern;           
                Log.i("dhiraj", " constructor");
            }

            @Override
            public Object getChild(int arg0, int arg1) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getChildId(int arg0, int arg1) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getChildView(int groupPosition, final int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {

                child=(ArrayList<String>)children.get(groupPosition);

                Log.i("dhiraj", "getChildView : " + child.toString());

                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) CustomerActivity.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    inflater.inflate(R.layout.activity_cust_child_list, null);
                }

                TextView tv1=(TextView)convertView.findViewById(R.id.tv_clildList_refe);
                tv1.setText(child.get(childPosition));

                return convertView;
            }

            @Override
            public int getChildrenCount(int arg0) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public Object getGroup(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public long getGroupId(int arg0) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {

                Log.i("dhiraj", "groupView");


                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) CustomerActivity.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    inflater.inflate(R.layout.activity_cust_group_list, null);

                }

                TextView tv_time = (TextView) convertView
                        .findViewById(R.id.tv_custList_Time);
                TextView tv_id = (TextView) convertView
                        .findViewById(R.id.tv_custList_id);
                TextView tv_name = (TextView) convertView
                        .findViewById(R.id.tv_custList_Name);

                tv_name.setText(this.parent.get(groupPosition));

                return convertView;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return false;
            }

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

        public void setGroupParents() {
            parentItems.add("Fruits");
            parentItems.add("Flowers");
            parentItems.add("Animals");
            parentItems.add("Birds");
            Log.i("dhiraj", "group items set");
        }

        // method to set child data of each parent
        public void setChildData() {

            // Add Child Items for Fruits
            ArrayList<String> child = new ArrayList<String>();
            child.add("Apple");
            child.add("Mango");
            child.add("Banana");
            child.add("Orange");

            childItems.add(child);

            // Add Child Items for Flowers
            child = new ArrayList<String>();
            child.add("Rose");
            child.add("Lotus");
            child.add("Jasmine");
            child.add("Lily");

            childItems.add(child);

            // Add Child Items for Animals
            child = new ArrayList<String>();
            child.add("Lion");
            child.add("Tiger");
            child.add("Horse");
            child.add("Elephant");

            childItems.add(child);

            // Add Child Items for Birds
            child = new ArrayList<String>();
            child.add("Parrot");
            child.add("Sparrow");
            child.add("Peacock");
            child.add("Pigeon");

            childItems.add(child);

            Log.i("dhiraj", "child items set");

        }
    }

2 个答案:

答案 0 :(得分:1)

请注意,在您的适配器中,重写的方法返回0,null .. 他们应该实际返回项目和项目计数,如果您将调试应用程序,您将看到适配器的计数始终为0,所以即使您已完成活动的所有连线,并添加了所有组和孩子的物品,你什么也看不见。

尝试更改getGroupCount()以返回groupItems.size(),同样更改getChildCount()

告诉我们是否解决了问题

答案 1 :(得分:1)

你的getChildrenCount返回0,你的getGroupCount也返回0.

您需要修改它以返回每个组的正确计数。