Updating layouts

As we have readied the object that will carry and persist the tweet status information, we can continue work with the UI.

First of all, we will update the stock_update_item.xml layout file by adding this:

<TextView
android:id="@+id/stock_item_twitter_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Very long Twitter update that is going
to be displayed as a message"
android:textColor="@android:color/holo_green_dark"
android:textSize="18sp" />

This will contain the Twitter status message. To make it display properly, we will have to add a root component:

<RelativeLayout
android:id="@+id/stock_item_content_block"
android:layout_width="match_parent"
android:layout_height="wrap_content">
[...]
</RelativeLayout>
<TextView
android:id="@+id/stock_item_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/stock_item_content_block"
android:layout_marginTop="5dp"
android:text="2012-12-01"
android:textSize="12sp" />

This will contain the following whole block:

<TextView
android:id="@+id/stock_item_symbol"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="GOOGLE"
android:textSize="18sp" />

<TextView
android:id="@+id/stock_item_twitter_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Very long Twitter update that is going
to be displayed as a message"
android:textColor="@android:color/holo_green_dark"
android:textSize="18sp" />

<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" />

Next, we will need to update StockUpdateViewHolder.java to have references to newly created elements in the layout:

@BindView(R.id.stock_item_twitter_status)
TextView twitterStatus;
[...]

public void setTwitterStatus(String twitterStatus) {
this.twitterStatus.setText(twitterStatus);
}

Also, when the Twitter status message is displayed, we should hide the quote name (at least for now) and the price of the stock. We can do this by adding a method in StockUpdateViewHolder.java, which, based on the type of the message, will hide the appropriate elements:

public void setIsStatusUpdate(boolean twitterStatusUpdate) {
if (twitterStatusUpdate) {
this.twitterStatus.setVisibility(View.VISIBLE);
this.price.setVisibility(View.GONE);
this.stockSymbol.setVisibility(View.GONE);
} else {
this.twitterStatus.setVisibility(View.GONE);
this.price.setVisibility(View.VISIBLE);
this.stockSymbol.setVisibility(View.VISIBLE);
}
}

Obviously, the onBindViewHolder() void needs to be updated to as follows in the StockDataAdapter.java as well:

@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());
holder.setTwitterStatus(stockUpdate.getTwitterStatus());
holder.setIsStatusUpdate(stockUpdate.isTwitterStatusUpdate());
}

Additionally, the add() method in  StockDataAdapter.java has to now take into account that some of the elements do not have quote information but have status updates:

for (StockUpdate stockUpdate : data) {
if (stockUpdate.getStockSymbol()
.equals(newStockUpdate.getStockSymbol())) {
if (stockUpdate.getPrice().equals(newStockUpdate.getPrice())
&& stockUpdate.getTwitterStatus().equals
(newStockUpdate.getTwitterStatus())) {
return;
}
break;
}
}

The code here takes O(n) time to check for duplicate entries. It can easily start consuming lots of resources when the history of entries becomes very long.

To fight this, it is possible to use TreeSet<> to do the checking for the existing entries or limit a number of tweets that can be kept on the list.

Now the UI is ready and the tweets can be seen as a part of the stream. It will be a mix of financial stock updates and status messages from Twitter and will look as illustrated:

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

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