我按字段顺序排序:Check List
我有一个WrapperClass,它实现了Comparable接口:
public class CheckListWrapper implements Comparable
{
public Boolean isChecked {get; set;}
public String shortDescription {get; set;}
public String fullDescription {get; set;}
public Integer order {get; set;}
public CheckList__c checkList;
public CheckListItem__c checkListItem;
public CheckListWrapper(CheckList__c chList)
{
shortDescription = chList.Short_Description__c;
fullDescription = chList.Full_Description__c;
order = (Integer)chList.Order__c;
isChecked = false;
checkList = chList;
}
public CheckListWrapper(CheckListItem__c chListItem)
{
shortDescription = chListItem.CheckList__r.Short_Description__c;
fullDescription = chListItem.CheckList__r.Full_Description__c;
order = (Integer)chListItem.CheckList__r.Order__c;
isChecked = true;
checkListItem = chListItem;
}
public Integer compareTo(Object compareTo)
{
CheckListWrapper compareToCheckList = (CheckListWrapper)compareTo;
Integer returnValue = 0;
if (checkList != null)
{
if (checkList.Order__c > compareToCheckList.checkList.Order__c)
{
returnValue = 1;
} else if (checkList.Order__c < compareToCheckList.checkList.Order__c)
{
returnValue = -1;
}
} else if (checkListItem != null)
{
if (checkListItem.CheckList__r.Order__c > compareToCheckList.checkListItem.CheckList__r.Order__c)
{
returnValue = 1;
} else if (checkListItem.CheckList__r.Order__c < compareToCheckList.checkListItem.CheckList__r.Order__c)
{
returnValue = -1;
}
}
return returnValue;
}
}
界面可比较有效,但所有选中的复选框都高于未选中状态。 问题是为什么选中复选框高于未选中状态以及如何避免它?
答案 0 :(得分:0)
好。我明白了我的错误:一切都没问题,但是我应该使用一个块,如果在方法compareTo中没有,也不要通过Object CheckList__c引用字段Order__c。我应该使用&#34; order&#34;来自CheckListWrapper的变量:
public Integer compareTo(Object compareTo)
{
CheckListWrapper compareToCheckList = (CheckListWrapper)compareTo;
Integer returnValue = 0;
if (order > compareToCheckList.order)
{
returnValue = 1;
} else if (order < compareToCheckList.order)
{
returnValue = -1;
}
return returnValue;
}