Handling multiple records on the UI

After an introduction of the regular updates of the stock quotes, it can soon be noticed that it generates lots of duplicate entries in the RecyclerView list.

The problem is that quite often the stock doesn't change right away, so we will see lots of duplicate or rather irrelevant entries.

This issue can be fixed by updating the .add() method of StockDataAdapter to as illustrated:

public void add(StockUpdate newStockUpdate) {
for (StockUpdate stockUpdate : data) {
if (stockUpdate.getStockSymbol().equals
(newStockUpdate.getStockSymbol())) {
if (stockUpdate.getPrice().equals
(newStockUpdate.getPrice())) {
return;
}
break;
}
}

this
.data.add(0, newStockUpdate);
notifyItemInserted(0);
}

What this new code basically does is that now the new entries are checked against the existing entries, and if there is no change as compared to the previous entry, the update is discarded.

It does that by finding the first (newest) stock from the list that matches the current symbol. When a match is found, the price is checked--if it is equal to what was present previously, the update is ignored, and the update is applied if it is different.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.111.87