How To Correctly Call A Method With Parameter Of Generic Type And Get Rid Of The "unchecked Call To Member Of Raw Type" Warning
I have created a base class for different subclasses of adapters for my list, which it has a List field with generic type. The code of the class is as below: public class ListBaseA
Solution 1:
For your example cases, you should also initialize them with a type, i.e.:
publicclassUserListAdapterextendsListBaseAdapter<Users> {...}
publicclassAddressListAdapterextendsListBaseAdapter<Address> {...}
If you want to maintain only a single reference but still be able to pass in specific types, then given the code you provided, what I'd recommend would be something along these lines:
publicclassTheListFragmentextendsListFragment {
// You really don't even need to keep a reference to this// since it can be retrieved with getListAdapter()private ListBaseAdapter<?> adapter;
publicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
...
switch(type) {
case1:
UserListAdapteruserAdapter=newUserListAdapter();
userAdapter.setDataset(users);
adapter = userAdapter;
break;
case2:
AddressListAdapteraddressAdapter=newAddressListAdapter();
addressAdapter.setDataset(addresses);
adapter = addressAdapter;
break;
}
setListAdapter(adapter);
}
}
Provided you don't need to make future data assignments to the adapter, that will do the trick. Your field is only of type ListBaseAdapter<?>
, but your local variables are of the specific type, so you can work with them directly, and then assign them to the more weakly-typed field adapter
.
Post a Comment for "How To Correctly Call A Method With Parameter Of Generic Type And Get Rid Of The "unchecked Call To Member Of Raw Type" Warning"