- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含这些部分的 ListView
。当有一个部分时,它就会取代另一个部分。也就是说,如果 ListView 项目计数为 30,则该部分将取代第一段,结果显示只有 29 点。
这里有一张图,很清楚
尝试了 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;
}
空缺模型
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 的计数?
最佳答案
您发送的 listItem 也应该有类型字段。所以我的建议是不要在 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;
}
}
在 Activity 中的 Activity 中执行以下操作:
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);
}
适配器类:
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();
}
关于java - 如何更改 ListView 项目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40781652/
driver.get("https://www.yahoo.com/"); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS)
原始 DataFrame 只有 1 列元组。我想添加一列,其中包含每个元组中的项目数(长度)。 ITEMS 0 (EGG, WATER, TOMATOE) 1 (MILK
我有一个包含这些部分的 ListView。当有一个部分时,它就会取代另一个部分。也就是说,如果 ListView 项目计数为 30,则该部分将取代第一段,结果显示只有 29 点。 这里有一张图,很清楚
这个问题在这里已经有了答案: parsing nested parentheses in python, grab content by level (4 个答案) 关闭 4 年前。 我想提取与 a
我决定使用 Gwan 的 KV 存储来代替 Redis NoSQL 数据库。我在手册中看到,kv 支持文件 IO 的持久标志,以在重新启动之间保留我猜的数据。它使用回调recfn,但是这个函数究竟如何
我现在正在学习 swift。我在我的 swift 代码中使用了 ANetworking (Obj C 中的库)。我已经成功返回解析后的 JSON。但是我想找到返回的 json 项目的数量。这是我到目前
我正在重复这样的 JSON 对象(嵌套)项目列表: {{specific}} 我想在页面顶部的每个项目对象中显示“特定”项目的数量(在 ng-repeat 范围之外)。我无
我目前有 8 个 UITabBar 项目,所有这些项目都可以在“更多” View 中进行编辑。我的问题是,允许的标签栏项目的绝对最大数量是多少? 我理解5是一次最多可见的;我正在寻找您能拥有的最长月经
如果我在 DynamoDB 中扫描或查询,则可以设置 Limit属性(property)。 DynamoDB documentation说如下: The maximum number of items
我想知道的是我们如何在 Angular js 中获取项目的 ng-repeat 计数。从下面的代码中,我想获得技能的数量,因为我想对其进行限制。如果 skill count > 5 将 skill 限
我在日志中收到此异常 - java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0
我是一名优秀的程序员,十分优秀!