好的,我们有一些领域结果返回我们已经制作的一组库存订单。根据是否完全检查库存订单,我们使用适配器中的if语句将行项目的背景颜色显示为绿色或蓝色。
问题是在加载时订单显示为绿色/蓝色正确。但是当你向下滚动时,蓝色的会变为绿色。 (见图片)。我们认为可能是某些地方的数据发生了变化,但不确定为什么会发生这种情况,因为我们只是在页面上滚动。
创建此代码的代码:
StockOrdersFragment.java
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.stock_orders_layout, container, false);
listView = myView.findViewById(R.id.listView);
stockOrdersService = new StockOrdersService(this);
stockOrdersService.fetchFromServer(getActivity());
fetchStockOrders();
return myView;
}
public void fetchStockOrders()
{
stockOrders.clear();
RealmResults<StockOrder> savedStockOrders = stockOrdersService.all();
stockOrders.addAll(savedStockOrders);
StockOrdersAdaptor adaptor = new StockOrdersAdaptor(stockOrders, getActivity());
listView.setAdapter(adaptor);
}
@Override
public void ordersReceived() {
fetchStockOrders();
}
StockOrdersAdaptor.java(这个颜色的位)
lastPosition = position;
if(stockOrder != null){
viewHolder.stockOrderId.setText(String.valueOf(stockOrder.id));
viewHolder.supplierName.setText(stockOrder.supplier.name);
if (stockOrder.allItemsHaveBeenReceived()) {
convertView.setBackgroundResource(R.color.colorGreen);
}
}
// Return the completed view to render on screen
return convertView;
allItemsHaveBeenRecieved()
public Boolean allItemsHaveBeenReceived()
{
for (StockOrderDetails detail: details) {
if (detail.quantity != detail.quantityReceived) {
return false;
}
}
return true;
}
答案 0 :(得分:1)
在此处添加else语句:
if (stockOrder.allItemsHaveBeenReceived()) {
convertView.setBackgroundResource(R.color.colorGreen);
} else {
convertView.setBackgroundResource(R.color.colorBlue);
}
问题是当你的物品被回收时,它仍然很脏。每次适配器绑定到列表项布局时,请确保填充所有 UI项目。