I have a ListView listing a custom object (let’s say MyObject).
I want to filter it dynamically through an EditText so I had to implement a getFilter() with a publishResults method:
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
MyObjectAdapter.this.setItems((List<MyObject>) results.values);
MyObjectAdapter.this.notifyDataSetChanged();
}
At this point, Eclipse complains: Type safety: Unchecked cast from Object to List<MyObject>
I am sure this cast will always be true, but Eclipse only suggests to add @SuppressWarnings("unchecked") but I’m totally against SuppressWarnings because it’s only hiding the problem, not a solution…
I tried adding:
if(results.values instanceof List<MyObject>)
But Eclipse complains again, and this solves nothing…
Cannot perform instanceof check against parameterized type List<MyObject>. Use the form List<?>
I know the casting will always be correct, but which is the proper way to make the code to be sure results.values is actually a List<MyObject> ?
Thanks in advance!
Well, I finally managed to find a solution.
Just as @Medo42 said:
Even though I did not went through the process of creating a whole new object in order to make this particular case to work “warning-less” this was the right direction to go.
So I took @lokoko ‘s idea and use it in a new
setItems()method, with anObjectparameter instead of aList<MyObject>in order to make sureThe result code is the following:
Thanks everyone for your help!