gpt4 book ai didi

java - 作为原始类型 ListAdapter 的成员,对submitList(List) 进行未经检查的调用

转载 作者:行者123 更新时间:2023-12-02 09:56:07 26 4
gpt4 key购买 nike

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

[unchecked] unchecked call to submitList(List) as a member of the raw type ListAdapter where T is a type-variable: T extends Object declared in class ListAdapter

这可能不重要,但它让我很恼火,我用作引用的那个从来没有围绕 ListAdapter 或任何东西有任何 <> 并且没有警告,所以我不确定为什么我的。

我的 fragment :

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

我的 View 模型:

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您将获得所需的类型信息。

关于java - 作为原始类型 ListAdapter 的成员,对submitList(List<T>) 进行未经检查的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55997207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com