【问题标题】:Unchecked call to submitList(List<T>) as a member of the raw type ListAdapter作为原始类型 ListAdapter 的成员对 submitList(List<T>) 的未经检查的调用
【发布时间】:2019-05-05 23:14:45
【问题描述】:

我一直在复制 Sunflower android 架构最佳实践应用程序,到目前为止速度很慢但很成功,虽然我的代码可以运行,但它显示一条未经检查的消息,所以我运行了它询问的命令,它是这样说的:

[unchecked] 未选中调用 submitList(List) 作为 原始类型 ListAdapter,其中 T 是类型变量:T extends Object 在 ListAdapter 类中声明

这可能并不重要,但它让我很烦,我用作参考的那个在 ListAdapter 周围从来没有任何 或任何东西,也没有任何警告,所以我不确定我的原因。

我的片段:

public class NewsFeedFragment extends Fragment {

    private PinViewModel viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FragmentNewsFeedBinding binding = FragmentNewsFeedBinding.inflate(inflater, container, false);
        PinViewModelFactory factory = InjectorUtils.providePinListViewModelFactory(getContext());
        ListAdapter adapter = new PinListAdapter();
        binding.pinList.setAdapter(adapter);
        this.viewModel = ViewModelProviders.of(this, factory).get(PinViewModel.class);
        subscribeUi(adapter);
        return binding.getRoot();
    }

    private void subscribeUi(ListAdapter adapter) {
        this.viewModel.pins.observe(getViewLifecycleOwner(), pins -> {
            if (pins != null) {
                adapter.submitList(pins);
            }
        });
    }
}

我的视图模型:

public class PinViewModel extends ViewModel {

    private PinRepository pinRepository;

    public LiveData<List<Pin>> pins;

    PinViewModel(@NonNull PinRepository pinRepository) {
        this.pinRepository = pinRepository;
        this.pins = this.pinRepository.getPins();
    }
}

我的适配器:

public class PinListAdapter extends ListAdapter<Pin, PinListAdapter.ViewHolder>{

    public PinListAdapter() {
        super(new PinDiffCallback());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ListItemPinBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Pin pin = getItem(position);
        holder.bind(pin);
        holder.itemView.setTag(pin);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ListItemPinBinding binding;

        ViewHolder(@NonNull ListItemPinBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(Pin item) {
            binding.setPin(item);
            binding.executePendingBindings();
        }
    }

    static class PinDiffCallback extends DiffUtil.ItemCallback<Pin> {

        @Override
        public boolean areItemsTheSame(@NonNull Pin oldItem, @NonNull Pin newItem) {
            return oldItem.getId() == (newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull Pin oldItem, @NonNull Pin newItem) {
            return oldItem == newItem;
        }
    }
}

【问题讨论】:

    标签: java android android-jetpack


    【解决方案1】:

    你正在使用

    ListAdapter adapter = new PinListAdapter();
    

    这是丢弃类型信息PinListAdapter。具体来说,ListAdapter&lt;Pin, PinListAdapter.ViewHolder&gt; 泛型类型信息。因此,您的 subscribeUi 不知道您的 ListAdapter 获取 Pin 对象的列表,并且您遇到了您遇到的错误。

    您可以将您的适配器更改为PinListAdapter,您将获得所需的类型信息。

    【讨论】:

      【解决方案2】:

      该示例是用 Kotlin 而非 Java 编写的...

      不清楚List&lt;T&gt; pins是什么;那应该是List&lt;Pin&gt; pins:

      if (pins != null && pins instanceof List<Pin>) {
          adapter.submitList(pins);
      }
      

      【讨论】:

      • 有人将该链接添加到 kotlin 之一(现在已更改),我使用的是 java 的,它不需要那个。我很高兴将其作为解决方案,但我很想知道为什么他不需要它而我的需要
      • 其实我已经意识到它显示“instanceof 的非法泛型类型”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      相关资源
      最近更新 更多