One will notice pretty quickly that it is quite unwieldy to work just with string data as the app will process stock updates. For the purpose of a proper way to track stock updates, we will create a Value object that will keep that information. It will contain the following fields for now:
- stockSymbol: to know which symbol we will be following
- currentPrice: the price as of the last update
- date: the date of the last update
The list might be a bit too naive for professional use, but it will serve as a starting point for upcoming work. The final class can look like this:
public class StockUpdate implements Serializable {
private final String stockSymbol;
private final BigDecimal price;
private final Date date;
public StockUpdate(String stockSymbol, double price, Date date) {
this.stockSymbol = stockSymbol;
this.price = new BigDecimal(price);
this.date = date;
}
public String getStockSymbol() {
return stockSymbol;
}
public BigDecimal getPrice() {
return price;
}
public Date getDate() {
return date;
}
}
Obviously, we then need to update other parts of the code to make use of this refactoring:
@Override
public void onBindViewHolder(StockUpdateViewHolder holder, int position) {
StockUpdate stockUpdate = data.get(position);
holder.setStockSymbol(stockUpdate.getStockSymbol());
holder.setPrice(stockUpdate.getPrice());
holder.setDate(stockUpdate.getDate());
}
Also, StockUpdateViewHolder now has additional methods and view fields:
@BindView(R.id.stock_item_date)
TextView date;
@BindView(R.id.stock_item_price)
TextView price;
private static final NumberFormat PRICE_FORMAT = new DecimalFormat("#0.00");
public void setStockSymbol(String stockSymbol) {
this.stockSymbol.setText(stockSymbol);
}
public void setPrice(BigDecimal price) {
this.price.setText(PRICE_FORMAT.format(price.floatValue()));
}
public void setDate(Date date) {
this.date.setText(DateFormat.format("yyyy-MM-dd hh:mm", date));
}
Also, our file stock_update_item.xml will change slightly to nicely display all this new data. The stock_item_symbol is now as shown:
<TextView
android:id="@+id/stock_item_symbol"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="GOOGLE"
android:textSize="18sp" />
At the same time, stock_item_price and stock_item_date are handled by the following:
<TextView
android:id="@+id/stock_item_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="18.90"
android:textColor="@android:color/holo_green_dark"
android:textSize="22sp" />
<TextView
android:id="@+id/stock_item_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/stock_item_symbol"
android:layout_marginTop="5dp"
android:text="2012-12-01"
android:textSize="12sp" />
Also, all the preceding elements will be wrapped inside the RelativeLayout that will be in the CardView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
[...]
</RelativeLayout>
</android.support.v7.widget.CardView>
Finally, our Observable--the piece that creates the data--will look like this:
Observable.just(
new StockUpdate("GOOGLE", 12.43, new Date()),
new StockUpdate("APPL", 645.1, new Date()),
new StockUpdate("TWTR", 1.43, new Date())
)