2

I have an auto complete adapter but i'm getting this warning: Unchecked cast: 'java.lang.Object' to 'java.util.ArrayList'

This is the code for my filter where i'm getting it:

private final Filter nameFilter = new Filter() {
    @Override
    public CharSequence convertResultToString(Object resultValue) {
        return ((UserNameAndPic) resultValue).getUserName();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestions.clear();
            for (UserNameAndPic people : tempItems) {
                if (people.getUserName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    suggestions.add(people);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
        List<UserNameAndPic> filterList = (List<UserNameAndPic>) results.values;
        if (results.count > 0) {
            clear();
            for (UserNameAndPic people : filterList) {
                add(people);
                notifyDataSetChanged();
            }
        }
    }
};

it has a problem with the line:

List<UserNameAndPic> filterList = (ArrayList<UserNameAndPic>) results.values;

I know its just a warning and i can suppress it but I want to avoid the casting and not suppress the warning. Any one knows what to do?

Liraz Shaka Amir
  • 587
  • 9
  • 26

1 Answers1

2

The compiler doesn't know if the casting you're doing is correct and safe or not.

If you can't avoid the casting (the best practice would be to avoid it, but you're not posting enough code to help) then you can just suppress the warning, like this:

@SuppressWarnings("unchecked")

You can do that at the method level, or even to the variable itself:

@SuppressWarnings("unchecked")
String v = (String) vToCast;

I'm sure this question has been answered millions of times anyway...

Magd Kudama
  • 3,229
  • 2
  • 21
  • 25
  • This can be done as Magd Kudama answered but is not really best practice. What about using this?: `String v = String.valueOf(vToCast);` in your case it is even advisable to check if the method `results.cast()` is usable to cast the result to the wanted object. – Nico Dec 07 '16 at 08:31
  • @Nico as I said, the best practice is really just make `results.values` return a `List`, therefore avoiding any casting at all. – Magd Kudama Dec 07 '16 at 08:40
  • sorry I overlooked that point from your statement. True indeed, it's best to avoid casting, but I thought since this is a implemented Java Object he seems to apply to his custom object the function `cast()` that is available for FilterResult is an option to perform a safe casting. Sorry again – Nico Dec 07 '16 at 09:06
  • 1
    I have edited my question and added my complete filter adapter. Can you help me figure out what to change so i can avoid casting? – Liraz Shaka Amir Dec 10 '16 at 19:08
  • Question is answered here: https://stackoverflow.com/questions/14642985/type-safety-unchecked-cast-from-object-to-listmyobject – Laurence Cooper Aug 31 '19 at 08:26