0
我想通過使用ArrayAdapter創建帶有標題的列表視圖。標題在加載時顯示正常行的日期。我正在交換xmls取決於條件。但是,當我嘗試向上滾動時,convertView返回null,有時不爲null。這是爲什麼發生?Listview自定義適配器convertView不一致
private Context context;
private ArrayList<MapExtension> mapValues;
private LinkedHashMap<Integer, String> dateLinkedHashMap;
ListViewAdapter_4(Context context, LinkedHashMap<Integer, MapExtension> mapValues) {
super(context, R.layout.custom_listview_layout_3_header, mapValues.keySet().toArray());
this.context = context;
this.mapValues= new ArrayList<>();
this.mapValues.addAll(mapValues.values());
this.dateLinkedHashMap = new LinkedHashMap<>();
}
private static class ViewHolder {
TextView lblStudentName;
TextView lblStudentNumber;
TextView lblDateSection;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder mViewHolder;
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.US);
String newDateStr = "";
if (convertView == null) {
mViewHolder = new ViewHolder();
if (dateLinkedHashMap.size() == 0) {
convertView = inflater.inflate(R.layout.custom_listview_layout_3_header, parent, false);
mViewHolder.lblDateSection = (TextView) convertView.findViewById(R.id.lblDateSection);
} else {
newDateStr = dateFormat.format(getDate(mapValues.get(position).getDate()));
if (dateLinkedHashMap.containsValue(newDateStr)) {
convertView = inflater.inflate(R.layout.custom_listview_layout_3, parent, false);
mViewHolder.lblStudentName= (TextView) convertView.findViewById(R.id.lblStudentName);
mViewHolder.lblStudentNumber = (TextView) convertView.findViewById(R.id.lblStudentNumber);
} else {
convertView = inflater.inflate(R.layout.custom_listview_layout_3_header, parent, false);
mViewHolder.lblDateSection = (TextView) convertView.findViewById(R.id.lblDateSection);
}
}
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
MapExtension me = mapValues.get(position);
String name = me.getStudentName();
String number = me.getStudentNumber();
if(mViewHolder.lblStudentName != null && mViewHolder.lblStudentNumber != null) {
mViewHolder.lblStudentName.setText(name);
mViewHolder.lblStudentNumber.setText(number);
}
// this block works fine earlier in regaining the position of the date
// but when the problem occurred, it never worked again.
if(mViewHolder.lblDateSection != null) {
newDateStr = dateFormat.format(getDate(date));
if (dateLinkedHashMap.size() == 0) {
mViewHolder.lblDateSection.setText(newDateStr);
dateLinkedHashMap.put(position, newDateStr);
} else {
if (dateLinkedHashMap.containsValue(newDateStr)) {
if (dateLinkedHashMap.containsKey(position)) {
mViewHolder.lblDateSection.setText(newDateStr);
} else {
mViewHolder.lblDateSection.setText("");
}
} else {
mViewHolder.lblDateSection.setText(newDateStr);
dateLinkedHashMap.put(position, newDateStr);
}
}
}
return convertView;
}
根據你正在嘗試做的,我想你應該使用檢查convertView == null,但也使用位置參數。大概看看Jeff Sharkey的分離列表適配器http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/ – chaitanya