2017-09-14 28 views
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; 
} 
+0

根据你正在尝试做的,我想你应该使用检查convertView == null,但也使用位置参数。大概看看Jeff Sharkey的分离列表适配器http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/ – chaitanya

回答

0

如果它代表一个行视图不再是可见的getView()方法通过convertView参数重新使用它来显示另一元件。

对于优化适配器将新数据分配给convertView而不是给一个新的数据充气。

如果没有查看供重复使用,空将发送作为获取视图方法convertView参数,它需要在viewHolder实施进行检查

相关问题