2013-11-10 49 views
0

我已經實現了一個自定義的ExpandableListView,但它沒有顯示任何數據。ExpandabelListView不顯示任何數據

這是我的佈局。

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"); 

     } 
    } 
+0

現在我收到其他錯誤。 http://wikisend.com/download/847830/log.txt。該錯誤是在線路'的TextView tv_time =(TextView的)convertView \t \t \t \t \t .findViewById(R.id.tv_custList_Time);' –

+0

得到它。我沒有初始化convertView –

回答

1

通知,在適配器中,被覆蓋的方法返回0,空.. 他們實際上應該返回的項目和項目數,如果你調試應用程序,您會發現適配器的計數總是爲0,因此即使您已完成活動的所有連線,並添加了所有組和子項目,也不會看到任何內容。

嘗試改變getGroupCount()返回groupItems.size()與同爲getChildCount()

讓我們知道,如果它解決了這一問題

+0

是的,這很好地解決了這個問題 –

1

你getChildrenCount將返回0,也是你的getGroupCount正在恢復0.

您需要修改它以返回每個組的正確計數。