我想在Scala插件eclipse中为android创建“searchView小部件”, 这是我的代码,
override def getFilter(): Filter = {
new Filter() {
protected override def publishResults(constraint: CharSequence, results:
FilterResults) {
var books = results.values.asInstanceOf[List[BookMetadata]]
ItemAdapter.this.notifyDataSetChanged()
}
protected override def performFiltering(constraint: CharSequence): FilterResults = {
val filteredResults = ListBuffer(books.asScala.toList.filter(b =>
b.toString.startsWith(constraint.toString)): _*) .asInstanceOf[List[BookMetadata]]
val results = new FilterResults()
results.values = filteredResults
results
}
}
}
我没有任何错误,但是当我运行程序时,我有这个ClassCastException
这是我的日志:
08-23 13:37:06.986: V/BookFragment(25407): query : modern
08-23 13:37:06.996: W/Filter(25407): An exception occured during performFiltering()!
08-23 13:37:06.996: W/Filter(25407): java.lang.ClassCastException:
scala.collection.mutable.ListBuffer cannot be cast to java.util.List
08-23 13:37:06.996: W/Filter(25407): at
com.bitlit.android.BooksFragment$ItemAdapter$$anon$2.
performFiltering(BooksFragment.scala:138)
08-23 13:37:06.996: W/Filter(25407): at
android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
08-23 13:37:06.996: W/Filter(25407): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-23 13:37:06.996: W/Filter(25407):
at android.os.Looper.loop(Looper.java:137)
08-23 13:37:06.996: W/Filter(25407):
at android.os.HandlerThread.run(HandlerThread.java:61)
请您给我一些关于此实施的提示
提前致谢!
答案 0 :(得分:0)
看一下这条线路似乎导致问题的原因是:
val filteredResults = ListBuffer(books.asScala.toList.filter(b =>
b.toString.startsWith(constraint.toString)): _*) .asInstanceOf[List[BookMetadata]]
你正在做这样的事情:
val filteredResults = ListBuffer(meta1, meta2).asInstanceOf[List[BookMetadata]]
因此ListBuffer[BookMetadata]
不是List[BookMetadata]
的实例。无论您是指java.util.List
还是scala immutable List
,都属于这种情况。
你可以这样做:
import collection.JavaConverters._
val filteredResults = ListBuffer(/* ... */).asJava
// should return an instance of java.util.List