當我嘗試在具有多個自定義佈局的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;
}
}
如果listView有一個方法來設置它們,你爲什麼要在適配器中使用頁眉和頁腳? – David
我在處理適配器中的頁腳和頭文件,因爲我可能想在頭文件或頁腳中放入一些數據,並且數據附帶了HTTP請求,並且稍後在我的活動中創建它時將結果傳遞給適配器。 –