1. Development tool android studio-2.3.31
2. Generics related issues
Today, I encountered a generic problem when replacing the code. The original JAVA code is as follows
public void upRecycle(RecyclerView recyclerView, LinkedList<MallBean> linkedList) {
try {
if (recyclerView.getAdapter() != null && recyclerView.getAdapter() instanceof MallAdapter) {
MallAdapter mallAdapter = (MallAdapter) recyclerView.getAdapter();
mallAdapter.setmData(linkedList);
mallAdapter.notifyDataSetChanged();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
The MallAdapter class is as follows
public class MallAdapter<T> extends BaseRecyclerAdapter<T> {
.........
}
That is to say, I didn't declare the specific type when I used it for initialization and assignment, and the generic type I used took up the pit, which is also usable.
This is what happens when it is automatically converted to Kotlin code
fun upRecycle(recyclerView: RecyclerView, linkedList: LinkedList<MallBean>) {
try {
if (recyclerView.adapter != null && recyclerView.adapter is MallAdapter<*>) {
val mallAdapter = recyclerView.adapter as MallAdapter<*>
mallAdapter.setmData(linkedList)
mallAdapter.notifyDataSetChanged()
}
} catch (ex: Exception) {
ex.printStackTrace()
}
}
At this time, **mallAdapter.setmData(linkedList)** is an error, and the error message is as follows
# Out-projected type 'MallAdapter<*>' prohibits the use of 'public open fun setmData(mDate: (Mutable)List<T!>!): Unit defined in MallAdapter'
The translation is to prohibit the use of the setmData method. The problem comes, why it can't be used. After a wave of operations, it is found that the mallAdapter object does not declare a specific object. After querying a wave of documents, the instructions on the use of generics are as follows, um, no Find a matching description about this (or if I don't understand it all say out, in, Any? or something, blame me)
Well, the solution is relatively simple, that is to declare the type when using it, that is, change the * number of the forced conversion to a specific type, which is as follows
val mallAdapter = recyclerView.adapter as MallAdapter<MallBean>
2. Use of common types When using common basic types in Kotlin, the first letter should be capitalized as follows java: int, boolean, float, double kotlin: Int, Boolean, Float, Double