gpt4 book ai didi

java - 如何更改 ListView 项目数?

转载 作者:行者123 更新时间:2023-11-30 00:52:21 28 4
gpt4 key购买 nike

我有一个包含这些部分的 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;
}

空缺模型

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com