2012-12-28 40 views
0

當我嘗試在具有多個自定義佈局的ListView中重用視圖時,會出現問題。當我更改數量的項目時,我得到錯誤convertView被傳遞給getView方法。詳細信息: 我有一個頁眉佈局,行佈局和頁腳佈局。在標題中,我可以更改行數。 1行的示例: 標頭 row1 頁腳 當我重新使用視圖時,我得到錯誤的結果(添加另一行後)。這是因爲先前創建頁腳視圖傳遞的位置= 2,因爲它不爲空,我不重建它: 頭 ROW1 頁腳 頁腳Android:如何重複使用具有動態數量項目和多個佈局的列表視圖項目

相反的預期: 頭 ROW1 2行 頁腳

我在做什麼錯?或者是設計,我應該重新創建視圖而不是重複使用它們? 這裏去我的代碼:

public class NewProgramAdapter extends BBBaseAdapter { 

public static int TYPE_HEADER = 1; 
public static int TYPE_FORM = 2; 
public static int TYPE_FOOTER = 3; 

protected String mDays; 
protected String mProgramId; 
protected int currentDay = 0; 
protected int numberOfExercises = 1; 
protected JSONArray items = new JSONArray(); 

public NewProgramAdapter(Activity a) { 
    super(a); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public int getCount() { 
    return numberOfExercises + 2; // header and footer 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (position > 0 && position < getCount() - 1) { 
     return getFormView(position, convertView, parent); 
    } else if (position == 0) { 
     return getHeaderView(position, convertView, parent); 
    } else { 
     return getFooterView(position, convertView, parent); 
    } 
} 

private View getFooterView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = mA.getLayoutInflater(); 
     v = inflater.inflate(R.layout.new_exercise_footer, null); 
    } 
    return v; 
} 

private View getHeaderView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = mA.getLayoutInflater(); 
     v = inflater.inflate(R.layout.new_exercise_header, null); 
     Spinner sp = (Spinner) v.findViewById(R.id.spinnerExercises); 
     sp.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parentView, 
        View selectedItemView, int position, long id) { 
       int n = position + 1; 
       setNumberOfExercises(n); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parentView) { 
       // your code here 
      } 

     }); 
    } 
    return v; 
} 

private View getFormView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = mA.getLayoutInflater(); 
     v = inflater.inflate(R.layout.new_exercise_form, null); 
    } 
    return v; 
} 

@Override 
public int getItemViewType(int position) { 
    if (position > 0 && position < getCount() - 1) { 
     return TYPE_FORM; 
    } else if (position == 0) { 
     return TYPE_HEADER; 
    } else { 
     return TYPE_FOOTER; 
    } 
} 

@Override 
public void updateEntries(Object data) { 
    items = (JSONArray) data; 
    notifyDataSetChanged(); 
} 

public void next() { 
    currentDay++; 
    notifyDataSetChanged(); 
} 

public void setNumberOfExercises(int n) { 
    numberOfExercises = n; 
    notifyDataSetChanged(); 
} 

public void setDays(String string) { 
    mDays = string; 
} 

public void setProgramId(String string) { 
    mProgramId = string; 
} 

}

+0

如果listView有一個方法來設置它們,你爲什麼要在適配器中使用頁眉和頁腳? – David

+0

我在處理適配器中的頁腳和頭文件,因爲我可能想在頭文件或頁腳中放入一些數據,並且數據附帶了HTTP請求,並且稍後在我的活動中創建它時將結果傳遞給適配器。 –

回答

3

看起來你忘了覆蓋getViewTypeCount()告訴使用多行佈局適配器。

但是,如果您只需要頁眉,頁腳和行佈局,那麼您應該使用ListView#addHeaderView()ListView#addFooterView()。不要試圖控制頁眉和頁腳是你的適配器,ListView已經爲你做了。

+0

謝謝,它似乎我錯過了getViewTypeCount()。我添加了返回3;聲明。但我收到一個錯誤: 12-28 21:25:18.163:E/AndroidRuntime(1128):致命異常:主 12-28 21:25:18.163:E/AndroidRuntime(1128):java.lang.ArrayIndexOutOfBoundsException:長度= 3;指數= 3 12-28 21:25:18.163:E/AndroidRuntime(1128):\t在android.widget.AbsListView $ RecycleBin.addScrapView(AbsListView.java:6437) 你可以建議的東西嗎? –

+1

我設法解決這個問題的幫助http://stackoverflow.com/questions/2596547/arrayindexoutofboundsexception-with-custom-android-adapter-for-multiple-views-in 視圖類型必須以0開頭。是個問題。 –

+0

你明白了,Adapter的RecycleBin需要一個從零開始的索引。很高興我能幫助(與原來的問題)! – Sam

相關問題