2014-03-05 44 views
2

我正在嘗試使用3種不同佈局的ExpandabelistView。子位置可擴展列表視圖返回錯誤的值?

的結構是這樣的: 如果是第一次佈局視圖加載應該firstview.xml 和他們,如果這是一個連孩子應該加載甚至有觀點認爲,如果是奇數應載入另一個。

問題是,它有時有效,但有時它不。我試圖記錄childPosition,但每次都會得到不同的值。孩子的位置值在同一個孩子內返回到0。

這裏是我的getChildView()

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

    final String children = (String) getChild(groupPosition, childPosition); 

    TextView textTitle; 
    TextView textDescription; 

    if(convertView == null) { 
     if(childPosition == 0) { 
      convertView = inflater.inflate(R.layout.listrow_first_element, null); 
     } else if(childPosition % 2 != 0) { 
      convertView = inflater.inflate(R.layout.listrow_details, null); 
     } else { 
      convertView = inflater.inflate(R.layout.listrow_details_color2, null); 
     } 
    } 

    textTitle = (TextView) convertView.findViewById(R.id.titleView); 
    textDescription = (TextView) convertView.findViewById(R.id.descriptionView); 
    textTitle.setText(getChildString(childPosition)); 
    textDescription.setText(children); 

    return convertView; 
} 

這裏是logcat的孩子們位置返回到我的代碼:

03-04 19:26:10.181 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0 
    03-04 19:26:10.181 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1 
    03-04 19:26:10.181 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 2 
    03-04 19:26:10.191 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 3 
    03-04 19:26:10.191 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 4 
    03-04 19:26:10.191 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 5 
    03-04 19:26:10.191 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6 
    03-04 19:26:10.201 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7 
    03-04 19:26:10.201 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0 
    03-04 19:26:10.211 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1 
    03-04 19:26:10.211 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 2 
    03-04 19:26:10.221 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 3 
    03-04 19:26:10.221 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 4 
    03-04 19:26:10.221 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 5 
    03-04 19:26:14.876 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4 
    03-04 19:26:14.876 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32409816 bytes) 
    03-04 19:26:15.096 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6 
    03-04 19:26:16.207 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc 
    03-04 19:26:19.601 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4 
    03-04 19:26:19.601 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32414792 bytes) 
    03-04 19:26:19.661 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7 
    03-04 19:26:20.001 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1 
    03-04 19:26:20.121 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0 
    03-04 19:26:20.952 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc 
    03-04 19:26:21.032 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4 
    03-04 19:26:21.032 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32416096 bytes) 
    03-04 19:26:21.152 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc 
    03-04 19:26:23.114 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4 
    03-04 19:26:23.114 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32416096 bytes) 
    03-04 19:26:23.154 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6 
    03-04 19:26:23.214 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7 
    03-04 19:26:23.555 23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0 
    03-04 19:26:23.645 23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc 
+0

什麼是支持你的組/子數據的數據結構?你可以發佈getChild和getGroup的代碼嗎? – WindsurferOak

回答

2

這是你的問題:

if(convertView == null) { 
    if(childPosition == 0) { 
     convertView = inflater.inflate(R.layout.listrow_first_element, null); 
    } else if(childPosition % 2 != 0) { 
     convertView = inflater.inflate(R.layout.listrow_details, null); 
    } else { 
     convertView = inflater.inflate(R.layout.listrow_details_color2, null); 
    } 
} 

getView()被調用時,convertView不會總是爲空,並且不總是相同的。 ListView的適配器將重用視圖,因爲它認爲合適,它無法區分您的視圖類型。

你的解決方案,其一是:

A.充氣查看每次(比普通型更加昂貴)

B.預膨脹的意見,然後將它們保存在備份您的適配器對象,並調用getView()(可能或多或少昂貴的A型)返回時,他們

C.不做你想要做什麼(不過這是一個愚蠢的選擇,所以忘了這一個)

除非你看到很重的perfo,否則我可能會跟着A去愛情問題。

+1

謝謝!我選擇了A選項。由於數據是從數據庫在線加載的,因此總是需要0.5s來加載。所以我無法區分每次創建視圖的表現。 –