将适配器代码从 Java 转换为 Kotlin 时出现问题

Issue when converted Adapter code from Java to Kotlin

我已经使用 IDE 将适配器代码从 java 转换为 kotlin,但我遇到了一个问题,ListFiler class 没有在 Kotlin 代码中被调用。

我在下面提到了 java 代码和等效的 kotlin 代码

在 java 中从第一 - 第二 - 第三

在 Koltin 代码中,第一个 - 第二个 - 第三个未在调试中命中

请问我这里可能做错了什么

谢谢 R

在java下面是流程

//FIRST - THIS FUNCTION IS CALLED WHICH GETS TO SECOND

public void applyCurrentFilter(Filter.FilterListener listener) {
    getFilter().filter(null, listener);
}


//SECOND - THIS IS CALLED WHICH GETS TO THIRD
@NonNull
@Override
public Filter getFilter() {
    return new ListFilter();
}



private class ListFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
    //****THIRD - GETS INSIDE THIS performFiltering METHOD
        FilterResults filterResults = new FilterResults();
        ArrayList<ServiceOrder> tempFilteredObjects = new ArrayList<>();

        if (currentOrderFilter.getServiceTypes().size() == 0 && !currentOrderFilter.isHideClaimed()) {
            tempFilteredObjects.addAll(originalObjects);
        }

        if (currentOrderFilter.getServiceTypes().size() > 0) {
            tempFilteredObjects = filterServiceTypeResults(currentOrderFilter, originalObjects);
        }

        if (currentOrderFilter.isHideClaimed()) {
            tempFilteredObjects = filterOutClaimedOrders(tempFilteredObjects, currentOrderFilter, originalObjects);
        }

        filterResults.values = tempFilteredObjects;
        filterResults.count  = tempFilteredObjects.size();

        return filterResults;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results != null && results.count > 0) {
            filteredObjects = (List<ServiceOrder>) results.values;
            notifyDataSetChanged();
        } else {
            filteredObjects.clear();
            notifyDataSetInvalidated();
        }
    }
}

在科特林中

//FIRST - THIS FUNCTION IS CALLED WHICH GETS TO SECOND
fun applyCurrentFilter(listener: FilterListener?) {
    filter.filter(null, listener)
}

//SECOND - THIS IS CALLED WHICH DOES NOT GET TO THIRD
override fun getFilter(): Filter =
    ListFilter()



private inner class ListFilter : Filter() {
    override fun performFiltering(constraint: CharSequence): FilterResults {
    //*****DOES NOT GET HERE
        val filterResults = FilterResults()
        var tempFilteredObjects = ArrayList<ServiceOrder>()
        if (currentOrderFilter.serviceTypes.size == 0 && !currentOrderFilter.isHideClaimed) {
            tempFilteredObjects.addAll(originalObjects)
        }
        if (currentOrderFilter.serviceTypes.size > 0) {
            tempFilteredObjects = filterServiceTypeResults(currentOrderFilter, originalObjects)
        }
        if (currentOrderFilter.isHideClaimed) {
            tempFilteredObjects = filterOutClaimedOrders(tempFilteredObjects,
                currentOrderFilter,
                originalObjects
            )
        }
        filterResults.values = tempFilteredObjects
        filterResults.count = tempFilteredObjects.size
        return filterResults
    }

    override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
        if (results != null && results.count > 0) {
            filteredObjects = (results.values as List<ServiceOrder>).toMutableList()
            notifyDataSetChanged()
        } else {
            filteredObjects.clear()
            notifyDataSetInvalidated()
        }
    }
}

请分享您的建议

谢谢 R

约束可能为空。尝试将方法参数更改为以下,也许可行

override fun performFiltering(charSequence: CharSequence?): Filter.FilterResults