作为原始类型列表适配器的成员对提交列表(<T>列表)的未选中调用



我一直在复制向日葵Android Architecture最佳实践应用程序,到目前为止,很慢但成功,而我的代码正常工作,它显示了一条未检查的消息,所以我运行了命令和它的要求是这样说的:

[未检查]作为提交列表(列表(的未检查的呼叫(列表( raw类型listAdapter t是类型变量:t扩展对象 在类litchadapter中声明

这可能并不重要,但它让我很烦,我用作参考的那个从未在litchAdapter或其他任何东西周围都有任何&lt;>没有警告,所以我不确定我为什么是。

我的片段:

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);
            }
        });
    }
}

我的ViewModel:

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;
        }
    }
}

您正在使用

ListAdapter adapter = new PinListAdapter();

那就是丢弃类型信息 PinListAdapter。具体而言,ListAdapter<Pin, PinListAdapter.ViewHolder>通用类型信息。因此,您的subscribeUi不知道您的ListAdapter列出了Pin对象的列表,并且您会收到您正在遇到的错误。

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

该示例是用kotlin编写的,而不是在java中写的...

目前尚不清楚什么是List<T> pins;可能应该是List<Pin> pins

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

最新更新