2016-11-24 66 views
1

我有一個ListView與部分。當有一個部分時,則代替另一個部分。也就是說,如果ListView項目數爲30,則該部分代替第一個段落,並且事實證明該顯示僅有29個點。何改變ListView項數?

這裏是一個圖象,它清楚地示出

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`s算?

+0

在getCount()方法你應該返回vacancyModelList.size()+你的sectio的數量n個標題。 – Beyka

+0

@Beyka,嘗試。錯誤:'IndexOutOfBoundsException:無效索引30,大小爲30' –

+0

if(GlobalData.LoadDate(getContext())== null),該行在做什麼? – HourGlass

回答

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(); 
     } 
+0

非常感謝過去沒有消失的事實。你是我的救星!謝謝! –