我一直在复制Sunflower android体系结构最佳实践应用程序,到目前为止运行缓慢但成功,尽管我的代码有效,但它显示的是未经检查的消息,因此我运行了它询问的命令,并说了这句话:
[unchecked]取消对对commitList(List)作为其成员的调用 原始类型ListAdapter,其中T是类型变量:T扩展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);
}
});
}
}
我的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;
}
}
}
答案 0 :(得分:1)
您正在使用
byte[] lenBytes = reader.ReadBytes(4);
Array.Reverse(lenBytes);
int len = BitConverter.ToInt32(lenBytes, 0);
byte[] bytes = reader.ReadBytes(len);
string str = Encoding.UTF8.GetString(bytes);
这将丢弃类型信息ListAdapter adapter = new PinListAdapter();
。具体来说,PinListAdapter
通用类型信息。因此,您的ListAdapter<Pin, PinListAdapter.ViewHolder>
不知道您的subscribeUi
包含ListAdapter
个对象的列表,并且会遇到错误。
您可以将适配器更改为Pin
,然后将获得所需的类型信息。
答案 1 :(得分:0)
该示例是用Kotlin编写的,而不是用Java语言编写的...
目前尚不清楚List<T> pins
是什么;可能应该是List<Pin> pins
:
if (pins != null && pins instanceof List<Pin>) {
adapter.submitList(pins);
}