複数のカスタムレイアウトを持つListViewでビューを再利用しようとすると、問題が発生します。アイテムの数を変更すると、getViewメソッドに渡されるconvertViewが間違ってしまいます。詳細:ヘッダーレイアウト、行レイアウト、フッターレイアウトがあります。ヘッダーでは、行数を変更できます。1行の例:ヘッダーrow1フッタービューを再利用すると、(別の行を追加した後)間違った結果が得られます。これは、以前に作成されたフッタービューがposition = 2に渡され、nullではないため再作成しないために発生します。Headerrow1 Footer Footer
予想される代わりに:ヘッダーrow1row2フッター
私は何が間違っているのですか?それとも、設計によるものであり、ビューを再利用するのではなく、ビューを再作成する必要がありますか?これが私のコードです:
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;
}
}