2013-04-06 31 views
4

我正在擴展列表視圖,但我無法獲得可擴展列表視圖,請幫助我在我的代碼中得到過錯謝謝推進如何在可擴展列表視圖中獲取視圖在我的情況下不顯示可擴展列表它顯示空白

CodeMainActicity.java

public class CodeMainActicity extends Activity { 
    private ExpandListAdapter ExpAdapter; 
    private ArrayList<ExpandListGroup> ExpListItems; 
    private ExpandableListView ExpandList; 
    public String[] groups = null; 
    public String[][] headers = null; 
    DisplayMetrics metrics; 
    int width; 

    public int GetDipsFromPixel(float paramFloat) { 
     return (int) (0.5F + paramFloat 
       * getResources().getDisplayMetrics().density); 
    } 

    public ArrayList<ExpandListGroup> SetStandardGroups() { 
     ArrayList<ExpandListGroup> localArrayList1 = new ArrayList<ExpandListGroup>(); 
     int i = 0; 
     if (i >= groups.length) { 
      return localArrayList1; 
     } 
     ExpandListGroup localExpandListGroup = new ExpandListGroup(); 
     localExpandListGroup.setName(groups[i]); 
     ArrayList<ExpandListChild> localArrayList2 = new ArrayList<ExpandListChild>(); 
     for (int j = 0; j < 10; j++) { 
      while (headers[i][j] == null) { 
       localExpandListGroup.setItems(localArrayList2); 
       localArrayList1.add(localExpandListGroup); 
       i++; 
       break; 
      } 
      ExpandListChild localExpandListChild = new ExpandListChild(); 
      localExpandListChild.setName(headers[i][j]); 
      localExpandListChild.setTag(null); 
      localArrayList2.add(localExpandListChild); 

     } 
     return localArrayList1; 

    } 

    public void onCreate(Bundle paramBundle) { 
     groups = new String[11]; 
     headers = ((String[][]) Array.newInstance(String.class, new int[] { 14, 
       10 })); 
     groups[0] = "Data Types"; 
     groups[1] = "Date Time"; 
     groups[2] = "Regular Expressions"; 
     groups[3] = "Class"; 
     groups[4] = "Collections"; 
     groups[5] = "Generics"; 
     groups[6] = "Reflection"; 
     groups[7] = "Security"; 
     groups[8] = "Design Patterns"; 
     groups[9] = "Thread"; 
     groups[10] = "File Stream"; 
     headers[0][0] = "Boxing and Unboxing"; 
     headers[0][1] = "Complex Type"; 
     headers[0][2] = "Enum"; 
     headers[0][3] = "Tuple type"; 
     headers[0][4] = "Hex to Int"; 
     headers[0][5] = "Nullable Integer"; 
     headers[0][6] = "Custom ToString()"; 
     headers[0][7] = "Checked() and Unchecked()"; 
     headers[0][8] = "Overflow"; 
     headers[0][9] = "Char methods"; 
     headers[1][0] = "Get between Days"; 
     headers[1][1] = "Add one week to current date"; 
     headers[1][2] = "DateTime Compare"; 
     headers[2][0] = "Email Validation"; 
     headers[2][1] = "URL validation"; 
     headers[2][2] = "Zipcode validation"; 
     headers[3][0] = "Access modifiers"; 
     headers[3][1] = "Inheritance"; 
     headers[3][2] = "Interface"; 
     headers[3][3] = "Method Overloading"; 
     headers[3][4] = "Overriding"; 
     headers[3][5] = "Abstract class"; 
     headers[4][0] = "Array List"; 
     headers[4][1] = "Hash Table"; 
     headers[4][2] = "Dictionary"; 
     headers[5][0] = "Simple Generic hierarchy"; 
     headers[5][1] = "Generic IEnumerable"; 
     headers[5][2] = "Generic Collection class"; 
     headers[6][0] = "AppDomain Setup"; 
     headers[6][1] = "Type.GetMethods"; 
     headers[6][2] = "Assembly Version Information"; 
     headers[7][0] = "MD5 encode"; 
     headers[7][1] = "File Checksum"; 
     headers[7][2] = "Password Encryption"; 
     headers[8][0] = "Facade Pattern"; 
     headers[8][1] = "Factory Pattern"; 
     headers[8][2] = "Observer Pattern"; 
     headers[8][3] = "Singleton Pattern"; 
     headers[8][4] = "Adapter Pattern"; 
     headers[9][0] = "Creating Thread"; 
     headers[9][1] = "Asynchronous Calls"; 
     headers[9][2] = "Mutex"; 
     headers[10][0] = "Display File contents"; 
     headers[10][1] = "Write to a file"; 
     headers[10][2] = "Append to File"; 
     super.onCreate(paramBundle); 
     requestWindowFeature(1); 
     setContentView(R.layout.codemainscreen); 
     Button localButton = (Button) findViewById(R.id.btnhome); 
     ExpandList = ((ExpandableListView) findViewById(R.id.ExpList)); 
     ExpListItems = SetStandardGroups(); 
     ExpAdapter = new ExpandListAdapter(this, ExpListItems); 
     metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     width = metrics.widthPixels; 
     ExpandList.setIndicatorBounds(width - GetDipsFromPixel(50.0F), width 
       - GetDipsFromPixel(10.0F)); 
     ExpandList.setAdapter(ExpAdapter); 
     ExpandList 
       .setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
        public boolean onChildClick(
          ExpandableListView paramAnonymousExpandableListView, 
          View paramAnonymousView, int paramAnonymousInt1, 
          int paramAnonymousInt2, long paramAnonymousLong) { 
         Bundle localBundle = new Bundle(); 
         localBundle.putString("group", 
           Integer.toString(paramAnonymousInt1)); 
         localBundle.putString("item", 
           Integer.toString(paramAnonymousInt2)); 
         Intent localIntent = new Intent(CodeMainActicity.this 
           .getApplicationContext(), 
           CodeDisplayAcitvity.class); 
         localIntent.putExtras(localBundle); 
         CodeMainActicity.this.startActivity(localIntent); 
         return false; 
        } 
       }); 
     localButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View paramAnonymousView) { 
       Intent localIntent = new Intent(CodeMainActicity.this 
         .getApplicationContext(), MainActivity.class); 
       CodeMainActicity.this.startActivity(localIntent); 
      } 
     }); 
    } 
} 

ExpandListAdapter:

 public class ExpandListAdapter extends BaseExpandableListAdapter { 
    private Context context; 
    private ArrayList<ExpandListGroup> groups; 

    public ExpandListAdapter(Context paramContext, 
      ArrayList<ExpandListGroup> paramArrayList) { 
     this.context = paramContext; 
     this.groups = paramArrayList; 
    } 

    public void addItem(ExpandListChild paramExpandListChild, 
      ExpandListGroup paramExpandListGroup) { 
     if (!this.groups.contains(paramExpandListGroup)) 
      this.groups.add(paramExpandListGroup); 
     int i = this.groups.indexOf(paramExpandListGroup); 
     ArrayList<ExpandListChild> localArrayList = ((ExpandListGroup) this.groups 
       .get(i)).getItems(); 
     localArrayList.add(paramExpandListChild); 
     ((ExpandListGroup) this.groups.get(i)).setItems(localArrayList); 
    } 

    public Object getChild(int paramInt1, int paramInt2) { 
     return ((ExpandListGroup) this.groups.get(paramInt1)).getItems().get(
       paramInt2); 
    } 

    public long getChildId(int paramInt1, int paramInt2) { 
     return paramInt2; 
    } 

    public View getChildView(int paramInt1, int paramInt2, 
      boolean paramBoolean, View paramView, ViewGroup paramViewGroup) { 
     ExpandListChild localExpandListChild = (ExpandListChild) getChild(
       paramInt1, paramInt2); 
     if (paramView == null) 
      paramView = ((LayoutInflater) this.context 
        .getSystemService("layout_inflater")).inflate(
        R.layout.expandlist_child_item, null); 
     TextView localTextView = (TextView) paramView 
       .findViewById(R.id.tvChild); 
     localTextView.setText(localExpandListChild.getName().toString()); 
     localTextView.setTag(localExpandListChild.getTag()); 
     return paramView; 
    } 

    public int getChildrenCount(int paramInt) { 
     return ((ExpandListGroup) this.groups.get(paramInt)).getItems().size(); 
    } 

    public Object getGroup(int paramInt) { 
     return this.groups.get(paramInt); 
    } 

    public int getGroupCount() { 
     return this.groups.size(); 
    } 

    public long getGroupId(int paramInt) { 
     return paramInt; 
    } 

    public View getGroupView(int paramInt, boolean paramBoolean, 
      View paramView, ViewGroup paramViewGroup) { 
     ExpandListGroup localExpandListGroup = (ExpandListGroup) getGroup(paramInt); 
     if (paramView == null) { 
      paramView = ((LayoutInflater) this.context 
        .getSystemService("layout_inflater")).inflate(
        R.layout.expandlist_group_item, null); 
     } 
     ((TextView) paramView.findViewById(R.id.btngo)) 
       .setText(localExpandListGroup.getName()); 
     return paramView; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 

    public boolean isChildSelectable(int paramInt1, int paramInt2) { 
     return true; 
    } 
} 

ExpandListChild:

public class ExpandListChild { 
    private String Name; 
    private String Tag; 

    public String getName() { 
     return this.Name; 
    } 

    public String getTag() { 
     return this.Tag; 
    } 

    public void setName(String paramString) { 
     this.Name = paramString; 
    } 

    public void setTag(String paramString) { 
     this.Tag = paramString; 
    } 
} 

ExpandListGroup:

 public class ExpandListGroup { 
    private ArrayList<ExpandListChild> Items; 
    private String Name; 

    public ArrayList<ExpandListChild> getItems() { 
     return this.Items; 
    } 

    public String getName() { 
     return this.Name; 
    } 

    public void setItems(ArrayList<ExpandListChild> paramArrayList) { 
     this.Items = paramArrayList; 
    } 

    public void setName(String paramString) { 
     this.Name = paramString; 
    } 
} 
+0

您是否使用過Google搜索並嘗試過任何「Expandable Listview」教程? – GrIsHu 2013-04-06 08:18:00

+0

是的,我已經嘗試過,但我無法找到我的代碼 – kamal 2013-04-06 08:23:39

+0

相關問題:[示例代碼爲ExpandableListView日期時間作爲標題](https://experiments-on-android.blogspot.com/b/post-preview?token= HAW4l0sBAAA.w4NYfvVaiPNkErNQjoeBnw.qVQnPkdBSCnak8XvBRgjWA&postId = 1207135633310116375&type = POST#!/ 2015/02/group-by-month-and-display-result-in.html) – kenorb 2015-02-19 23:07:43

回答

6

SetStandardGroups() - 您只創建一個ExpandListGroup的實例,並添加項目到該組。您沒有將第二組項添加到結果ArrayList(localArrayList1)。嘗試使用以下代碼...

public ArrayList<ExpandListGroup> SetStandardGroups() 
{ 
    ArrayList<ExpandListGroup> resExpList = new ArrayList<ExpandListGroup>(); 

    if(groups != null && groups.length > 0) 
    { 
     for(int i=0; i<groups.length; i++) 
     { 
      ExpandListGroup expListGroup = new ExpandListGroup(); 
      expListGroup.setName(groups[i]); 
      Log.e("ExpList", "Group "+i+" Name="+groups[i]); 

      ArrayList<ExpandListChild> expListItems = new ArrayList<ExpandListChild>(); 
      for(int j=0; j<headers[i].length; j++) 
      { 
       ExpandListChild item = new ExpandListChild(); 
       item.setName(headers[i][j]); 
       Log.e("ExpList", "Item "+j+" ="+headers[i][j]); 

       item.setTag(null); 
       expListItems.add(item); 
      } 
      expListGroup.setItems(expListItems); 
      Log.e("ExpList", "Group "+i+" items size="+expListGroup.size()); 

      resExpList.add(expListGroup); 
     } 
    } 

    Log.e("ExpList", "Total groups size="+resExpList.size()); 
    return resExpList; 
} 
+0

我更新了最新的代碼..試試一次.. – Santhosh 2013-04-09 05:43:12

+0

nul指針例外ExpandListAdapter ((TextView)paramView.findViewById(R.id.btngo)).java。setText(localExpandListGroup.getName()); – kamal 2013-04-09 05:46:19

+0

04-09 05:44:30.057:E/AndroidRuntime(255):未捕獲的處理程序:由於未捕獲的異常導致主線程退出 04-09 05:44:30.124:E/AndroidRuntime(255):java.lang.NullPointerException 04-09 05:44:30.124:E/AndroidRuntime(255):\t at com.example.apktesting.ExpandListAdapter.getGroupView(ExpandListAdapter.java:81) – kamal 2013-04-09 05:47:56