何更改ListView项目计数?

时间:2016-11-24 08:50:24

标签: java android listview

我有ListView部分。当有一个部分时,它取代了另一个部分。也就是说,如果ListView项目计数为30,则该部分取代第一段,并且结果显示该节目仅为29分。

这是一张清晰显示的图片

enter image description here

尝试TYPE_MAX_COUNT = 2,3。不要混淆。

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

我认为getViewTypeCount()之后会触发getView()

private List<VacancyModel> vacancyModelList;
    private LayoutInflater inflater;
    private SQLHelper sqlHelper;

    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_ITEM = 0;
    private int rowType;

    public static String saveLastDate;
    private int newRecs = 0;

    public SuitableAdapter(Context context, int resource, List<VacancyModel> objects) {
        super(context, resource, objects);
        vacancyModelList = objects;

        sqlHelper = new SQLHelper(getContext());

        inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
        ViewHolder holder = null;

        rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();

            switch (rowType) {
                case TYPE_SEPARATOR:
                    convertView = inflater.inflate(R.layout.suitable_separator_layout, null);
                    holder.headerTv = (TextView) convertView.findViewById(R.id.section_header);

                    break;

                case TYPE_ITEM:
                    convertView = inflater.inflate(R.layout.row_layout, null);
                    holder.tvProfession = (TextView) convertView.findViewById(R.id.tvProfession);
                    holder.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader);
                    holder.tvSalary = (TextView) convertView.findViewById(R.id.tvSalary);
                    holder.tvDate = (TextView) convertView.findViewById(R.id.tvPostCr);
                    break;
            }

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (getItemViewType(position) == TYPE_SEPARATOR) {

            holder.headerTv = (TextView) convertView.findViewById(R.id.section_header);

            if (newRecs == 1) {
                holder.headerTv.setText("Новые вакансии");
                newRecs = 0;
            } else {
                holder.headerTv.setText("Ранее просмотренные");
            }
        }

        if (getItemViewType(position) == TYPE_ITEM) {

            final VacancyModel model = vacancyModelList.get(position);

            holder.tvProfession.setText(model.getProfession());
            holder.tvHeader.setText(model.getHeader());
            holder.tvSalary.setText(model.getSalary());
            holder.tvDate.setText(model.getDate());

            Date date;
            try {
                if (saveLastDate == null) {
                    saveLastDate = model.getDate();
                } else {
                    date = stringToDate(saveLastDate);
                    if (date.before(stringToDate(model.getDate()))) {
                        saveLastDate = model.getDate();
                    }
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if (GlobalData.LoadDate(getContext()) == null) {
            return TYPE_ITEM;
        } else {
            VacancyModel model = getItem(position);

            if (model != null) {
                String newString = model.getDate();
                String lastString = GlobalData.LoadDate(getContext());

                Date newDate = null;
                Date lastDate = null;

                try {
                    newDate = stringToDate(newString);
                    lastDate = stringToDate(lastString);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                assert newDate != null;

                if (newDate.equals(lastDate)) {
                    return TYPE_SEPARATOR;
                } else if (position == 0 && newDate.after(lastDate)) {
                    newRecs = 1;
                    return TYPE_SEPARATOR;
                } else {
                    return TYPE_ITEM;
                }
            } else {
                return TYPE_ITEM;
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getCount() {
        return vacancyModelList.size();
    }

    private Date stringToDate(String string) throws ParseException {
        return new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"), Locale.getDefault()).parse(string);
    }

    private static class ViewHolder {
        private TextView tvProfession;
        private TextView tvHeader;
        private TextView tvSalary;
        private TextView tvDate;

        private TextView headerTv;
    }

VacancyModel

public class VacancyModel implements Serializable{
    private String profession;
    private String header;
    private String salary;
    private String date;


    public VacancyModel() {
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getSalary() {
        if (salary.equals("0") || salary.isEmpty() || salary.equals("null")){
            return "empty";
        }
        else {
            return salary;
        }
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

问题:我做错了什么以及如何更改ListView的计数?

1 个答案:

答案 0 :(得分:1)

您发送的listItem也应该包含type字段。所以我建议的不是在listview中进行日期检查操作。您可以在将数据添加到RecyclerView之前执行此操作。

在可序列化数据中再添加两个字段:

public class VacancyModel implements Serializable{
    private String profession;
    private String header;
    private String salary;
    private String date;
   // set setter and getter for both, by default isHeading will be false,
    private boolean isHeading;
    private String heading;


    public VacancyModel() {
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getSalary() {
        if (salary.equals("0") || salary.isEmpty() || salary.equals("null")){
            return "empty";
        }
        else {
            return salary;
        }
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

在活动内的活动中执行以下操作:

   private void generateListHeading(List<VacancyModel> original_vacany_list)
    {

      List<VacancyModel> vacancy_type_new_record;
     VacancyModel model = new vacacyModel();
      model.setIsHeading(true); 
      model.setHeading("New Record");
    vacancy_type_new_record.add(model);
    List<VacancyModel> vacancy_type_watched;
    model = new vacacyModel();
      model.setIsHeading(true); 
      model.setHeading("Watched");
    vacancy_type_watched.add(model);

      List<VacancyModel> new_vacancy_list;
    for(VacanyModel  data:original_vacany_list)
    {
       //do your date condition check here
       if(data.getDate==newDate)
      {
         vacancy_type_new_record.add(data) 
      } else
      {
           vacancy_type_watched.add(data) 
      }
    }
    //once the whole condition check is add both list to new list
    new_vacancy_list.addAll(vacancy_type_new_record); 
    new_vacancy_list.addAll(vacancy_type_watched); 
    //now the item count will be 32. in the format heading ,data ,heading,data
    adapter.setUpdateddata(new_vacancy_list);
      }

<强> Adapter.class:

  vacanyModelList = new ArrayList<>();
    public SuitableAdapter(Context context, int resource) {
            super(context, resource, objects);
            //don't set your object in constructor
            sqlHelper = new SQLHelper(getContext());

            inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @NonNull
        @Override
        public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
            ViewHolder holder = null;

            rowType = getItemViewType(position);

            if (convertView == null) {
                holder = new ViewHolder();

                switch (rowType) {
                    case TYPE_SEPARATOR:
                        convertView = inflater.inflate(R.layout.suitable_separator_layout, null);
                        holder.headerTv = (TextView) convertView.findViewById(R.id.section_header);

                        break;

                    case TYPE_ITEM:
                        convertView = inflater.inflate(R.layout.row_layout, null);
                        holder.tvProfession = (TextView) convertView.findViewById(R.id.tvProfession);
                        holder.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader);
                        holder.tvSalary = (TextView) convertView.findViewById(R.id.tvSalary);
                        holder.tvDate = (TextView) convertView.findViewById(R.id.tvPostCr);
                        break;
                }

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (getItemViewType(position) == TYPE_SEPARATOR) {

                holder.headerTv = (TextView) convertView.findViewById(R.id.section_header);

                if (newRecs == 1) {
                    holder.headerTv.setText("Новые вакансии");
                    newRecs = 0;
                } else {
                    holder.headerTv.setText("Ранее просмотренные");
                }
            }

            if (getItemViewType(position) == TYPE_ITEM) {

                final VacancyModel model = vacancyModelList.get(position);

                holder.tvProfession.setText(model.getProfession());
                holder.tvHeader.setText(model.getHeader());
                holder.tvSalary.setText(model.getSalary());
                holder.tvDate.setText(model.getDate());

                Date date;
                try {
                    if (saveLastDate == null) {
                        saveLastDate = model.getDate();
                    } else {
                        date = stringToDate(saveLastDate);
                        if (date.before(stringToDate(model.getDate()))) {
                            saveLastDate = model.getDate();
                        }
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            return convertView;
        }

        @Override
        public int getItemViewType(int position) {
          VacancyModel model = getItem(position);
            if (model.isHeading) {
                return TYPE_SEPARATOR;
            } else {
              return TYPE_ITEM;
                   }
            }
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getCount() {
            return vacancyModelList.size();
        }

        private Date stringToDate(String string) throws ParseException {
            return new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"), Locale.getDefault()).parse(string);
        }

        private static class ViewHolder {
            private TextView tvProfession;
            private TextView tvHeader;
            private TextView tvSalary;
            private TextView tvDate;

            private TextView headerTv;
        }

        public void setUpdatedData(List<VacancyModel> updated_list)
        {
          this.vacancyModelList = updated_list;
          notifyDataSetChanged();
        }