kotlin - 需要说明:类型不匹配需要:未找到:MyItem
问题描述
目前我将我的 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
}
}
我的灵活适配器扩展的 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 中)没有显示该错误。
解决方案
在这一行中:private lateinit var myLayoutAdapter: MultiPurposeListAdapter<*>
您只需要指定一个 Item 类型(java 不需要它)例如:MultiPurposeListAdapter<MyItem>
或MultiPurposeListAdapter<AbstractFlexibleItem<*>>
. 有关详细信息,请参阅泛型类型概述
推荐阅读
- php - 我怎样才能使 PHP 只写入一次被选中的列表?
- android - 配备 M1 Max 芯片的新 MacBook Pro 上的 Android Studio?
- regex - 正则表达式匹配shell脚本中数组中的数字
- regex - 使用 Blueprism 提取正则表达式
- c - Makefile:计算更新的目标文件
- powerbi - Power Bi Embedded A SKU(恢复/暂停)
- python - 选择正确的范例来实现特定的编程任务
- charts - 水晶报表-如何为水晶报表图表添加过滤器
- php - WPML 翻译只显示一种语言的帖子
- html - CSS - 内联 SVG 会干扰行高?