Listview自定义适配器convertView不一致

时间:2017-09-14 18:28:01

标签: android listview android-arrayadapter

我正在尝试使用ArrayAdapter创建带有标头的listview。标题在加载时成功显示正常行的日期。我根据条件与xmls交换。但是,当我尝试向上滚动时,convertView返回null,有时不返回null。为什么会这样?

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;
}

1 个答案:

答案 0 :(得分:0)

如果表示行的视图不再可见,则getView()方法通过convertView参数重用它以显示另一个元素。

对于ptimization适配器,将新数据分配给convertView,而不是给新的数据充气。

如果没有View可用于重用,则null将作为getView方法中的convertView参数发送,需要在viewHolder实现中进行检查