import android.widget.TextView; //导入方法依赖的package包/类
/**
* Updates the total and how it's broken down.
*
* @param cart The shopping cart contents and the total.
*/
public void update(ShoppingCart cart) {
Context context = mBreakdownLayout.getContext();
CharSequence totalPrice = createValueString(
cart.getTotal().getCurrency(), cart.getTotal().getPrice(), true);
// Show the updated text view if the total changed.
showUpdateIfTextChanged(totalPrice);
// Update the summary to display information about the total.
setSummaryText(cart.getTotal().getLabel(), totalPrice);
mBreakdownLayout.removeAllViews();
if (cart.getContents() == null) return;
int maximumDescriptionWidthPx =
((View) mBreakdownLayout.getParent()).getWidth() * 2 / 3;
// Update the breakdown, using one row per {@link LineItem}.
int numItems = cart.getContents().size();
mBreakdownLayout.setRowCount(numItems);
for (int i = 0; i < numItems; i++) {
LineItem item = cart.getContents().get(i);
TextView description = new TextView(context);
ApiCompatibilityUtils.setTextAppearance(description, item.getIsPending()
? R.style.PaymentsUiSectionPendingTextEndAligned
: R.style.PaymentsUiSectionDescriptiveTextEndAligned);
description.setText(item.getLabel());
description.setEllipsize(TruncateAt.END);
description.setMaxLines(2);
if (maximumDescriptionWidthPx > 0) {
description.setMaxWidth(maximumDescriptionWidthPx);
}
TextView amount = new TextView(context);
ApiCompatibilityUtils.setTextAppearance(amount, item.getIsPending()
? R.style.PaymentsUiSectionPendingTextEndAligned
: R.style.PaymentsUiSectionDescriptiveTextEndAligned);
amount.setText(createValueString(item.getCurrency(), item.getPrice(), false));
// Each item is represented by a row in the GridLayout.
GridLayout.LayoutParams descriptionParams = new GridLayout.LayoutParams(
GridLayout.spec(i, 1, GridLayout.END),
GridLayout.spec(0, 1, GridLayout.END));
GridLayout.LayoutParams amountParams = new GridLayout.LayoutParams(
GridLayout.spec(i, 1, GridLayout.END),
GridLayout.spec(1, 1, GridLayout.END));
ApiCompatibilityUtils.setMarginStart(amountParams,
context.getResources().getDimensionPixelSize(
R.dimen.payments_section_descriptive_item_spacing));
mBreakdownLayout.addView(description, descriptionParams);
mBreakdownLayout.addView(amount, amountParams);
}
}