要求说明:类型不匹配要求:找不到内容:MyItem

时间:2019-08-22 04:14:50

标签: kotlin

目前,我将自己的android应用转换为Kotlin。该应用程序当前使用名为“ FlexibleAdapter”的第三方库。 我将适配器扩展名转换为Kotlin,没有任何错误。 但是,当我尝试使用此适配器时,在调用方法addItem()时出现类型不匹配错误,而适配器扩展未覆盖该方法。

由于缺乏Kotlin经验,我不明白这里出了什么问题以及如何解决。 有人可以解释我需要更改的内容吗?

我将代码精简为最基本的内容,以了解发生了什么! myfragment包含出现错误的代码

片段的Kotlin实现

class myfragment : Fragment() {
private lateinit var myLayoutAdapter: MultiPurposeListAdapter<*>
private var myItems = mutableListOf<AbstractFlexibleItem<*>>()

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    myLayoutAdapter = MultiPurposeListAdapter(myItems, this, true)

    // Prepare the RecyclerView and attach the Adapter to it
    fragment_recycler_view.apply {
        layoutManager = createNewLinearLayoutManager()
        adapter = mIngredientsLayoutAdapter
        setHasFixedSize(false) //Size of RecyclerView will change
    }
}

/******
 * A function to add items
 ******
fun addNewItem(itemToAdd: MyItem) {
    //Note: itemToAdd is from MyItem that extends AbstractFlexibleItem

    // If I try to invoke the addOtem method from the FlexibleAdapter
    // I get a Type mismatch error
    // addItem is not overriden by MultiplePurposeListAdapter!

    myLayoutAdapter.addItem(itemToAdd)  // <- Here I get a Type mismatch error
                                        // Required: Nothing
                                        // Found: MyItem
    }
}

我的Flexible Adapter扩展的Kotlin实现

open class MultiPurposeListAdapter<T: AbstractFlexibleItem<*>>
@JvmOverloads constructor(items: List<T>?, listeners: Any? = null, stableIds: Boolean = true)
: FlexibleAdapter<T>(items, listeners, stableIds) {

    // Here we extend the FlexibleAdapter with custom filters
    }

适配器的Java实现来自名为FlexibleAdapter的第三方库 注意:我只删除似乎需要理解的内容!

public class FlexibleAdapter<T extends IFlexible> extends AnimatorAdapter {

private List<T> mItems // The main container for ALL items

/**
 * Simply append the provided item to the end of the list.
 * <p>Convenience method of {@link #addItem(int, IFlexible)} with
 * {@code position = getMainItemCount()}.</p>
 *
 * @param item the item to add
 * @return true if the internal list was successfully modified, false otherwise
 */
public boolean addItem(@NonNull T item) {
    return addItem(getItemCount(), item);
}

/**
 * Returns the total number of items in the data set held by the adapter.
 *
 * @return the total number of items (headers and footers INCLUDED) held by the adapter
 */
@Override
public int getItemCount() {
    return mItems.size();
}

/**
 * Inserts the given item at the specified position or Adds the item to the end of the list
 * (no matters if the new position is out of bounds!).
 *
 * @param position position inside the list, if negative, items will be added to the end
 * @param item     the item to add
 * @return true if the internal list was successfully modified, false otherwise
 */
public boolean addItem(@IntRange(from = 0) int position, @NonNull T item) {
    if (item == null) {
        log.e("addItem No item to add!");
        return false;
    }
    log.v("addItem delegates addition to addItems!");
    return addItems(position, Collections.singletonList(item));
}

/**
 * Inserts a set of items at specified position or Adds the items to the end of the list
 * (no matters if the new position is out of bounds!).
 *
 * @param position position inside the list, if negative, items will be added to the end
 * @param items    the set of items to add
 * @return true if the internal list was successfully modified, false otherwise
 */
public boolean addItems(@IntRange(from = 0) int position, @NonNull List<T> items) {
    if (items == null || items.isEmpty()) {
        log.e("addItems No items to add!");
        return false;
    }
    int initialCount = getMainItemCount(); // Count only main items!
    if (position < 0) {
        log.w("addItems Position is negative! adding items to the end");
        position = initialCount + mScrollableHeaders.size();
    }
    // Insert the items properly
    performInsert(position, items, true);
    // Show the headers of new items
    showOrUpdateHeaders(items);
    // Call listener to update EmptyView
    if (!recursive && mUpdateListener != null && !multiRange && initialCount == 0 && getItemCount() > 0) {
        mUpdateListener.onUpdateEmptyView(getMainItemCount());
    }
    return true;
}
}

Android Studio内部的错误消息是: 类型不匹配。 必填:无 找到:MyItem

我希望是AbstractFlexibleItem。 这仅在Kotlin转换的代码中发生。其他片段实现(在Java中)没有显示该错误。

1 个答案:

答案 0 :(得分:0)

在这一行:private lateinit var myLayoutAdapter: MultiPurposeListAdapter<*>中,您只需指定一个项目类型(java不需要),例如:MultiPurposeListAdapter<MyItem>MultiPurposeListAdapter<AbstractFlexibleItem<*>>。有关更多信息,请参见generic types overview