package com.seandbeach.stockticker;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * {@link StockAdapter} exposes a list of weather forecasts
 * from a {@link android.database.Cursor} to a {@link android.widget.ListView}.
 */
public class StockAdapter extends CursorAdapter {
    public StockAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    // This is ported from FetchStockTask --- but now we go straight from the cursor to the string.
    private String convertCursorRowToUXFormat(Cursor cursor) {
        String name = cursor.getString(StockQuoteFragment.COL_STOCK_NAME);

        return cursor.getString(StockQuoteFragment.COL_STOCK_SYMBOL)
                + (name != null && !name.isEmpty() && !name.equals("null") ? (" (" + name + "): ") : ": ")
                + cursor.getDouble(StockQuoteFragment.COL_STOCK_PRICE)
                + " ("
                    + cursor.getDouble(StockQuoteFragment.COL_STOCK_CHANGE)
                    + ", " + cursor.getDouble(StockQuoteFragment.COL_STOCK_CHANGE_PERCENT) + "%"
                + ")";
    }

    // Remember that these views are reused as needed.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_quote, parent, false);

        return view;
    }

    // This is where we fill-in the views with the contents of the cursor.
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // our view is pretty simple here --- just a text view
        // we'll keep the UI functional with a simple (and slow!) binding.
        TextView tv = (TextView)view;
        tv.setText(convertCursorRowToUXFormat(cursor));
    }
}

@@ -257,16 +257,49 @@
 tView;%0A%0A
+import java.text.DecimalFormat;%0A%0A
 /**%0A * %7B
@@ -601,708 +601,745 @@
     
-// This is ported from FetchStockTask --- but now we go straight from the cursor to the string.%0A    private String convertCursorRowToUXFormat(Cursor cursor) %7B%0A        String name = cursor.getString(StockQuoteFragment.COL_STOCK_NAME);%0A%0A        return cursor.getString(StockQuoteFragment.COL_STOCK_SYMBOL)%0A                + (name != null && !name.isEmpty() && !name.equals(%22null%22) ? (%22 (%22 + name + %22): %22) : %22: %22)%0A                + cursor.getDouble(StockQuoteFragment.COL_STOCK_PRICE)%0A                + %22 (%22%0A                    + cursor.getDouble(StockQuoteFragment.COL_STOCK_CHANGE)%0A                    + %22, %22 + cursor.getDouble(StockQuoteFragment.COL_STOCK_CHANGE_PERCENT) + %22%25%22%0A                + %22)%22;
+public static class StockQuoteHolder %7B%0A        public final TextView symbolView;%0A        public final TextView nameView;%0A        public final TextView priceView;%0A        public final TextView changeView;%0A        public final TextView changePercentView;%0A%0A        public StockQuoteHolder(View view) %7B%0A            symbolView = (TextView) view.findViewById(R.id.list_item_stock_symbol);%0A            nameView = (TextView) view.findViewById(R.id.list_item_stock_name);%0A            priceView = (TextView) view.findViewById(R.id.list_item_quote_price);%0A            changeView = (TextView) view.findViewById(R.id.list_item_quote_change);%0A            changePercentView = (TextView) view.findViewById(R.id.list_item_quote_change_percent);%0A        %7D
 %0A   
@@ -1586,16 +1586,126 @@
 false);%0A
+        StockQuoteHolder stockQuoteHolder = new StockQuoteHolder(view);%0A        view.setTag(stockQuoteHolder);
 %0A       
@@ -1896,229 +1896,1094 @@
     
-// our view is pretty simple here --- just a text view%0A        // we'll keep the UI functional with a simple (and slow!) binding.%0A        TextView tv = (TextView)view;%0A        tv.setText(convertCursorRowToUXFormat(cursor)
+StockQuoteHolder holder = (StockQuoteHolder) view.getTag();%0A%0A        String stockSymbol = cursor.getString(StockQuoteFragment.COL_STOCK_SYMBOL);%0A        holder.symbolView.setText(stockSymbol);%0A%0A        String stockName = cursor.getString(StockQuoteFragment.COL_STOCK_NAME);%0A        holder.nameView.setText(stockName);;%0A%0A        String pricePattern = %22%5Cu00A4#,##0.00%22;%0A        DecimalFormat fmt = new DecimalFormat(pricePattern);%0A        double quotePrice = cursor.getDouble(StockQuoteFragment.COL_STOCK_PRICE);%0A        holder.priceView.setText(fmt.format(quotePrice));%0A%0A        String changePattern = %22#,##0.00%22;%0A        fmt.applyPattern(changePattern);%0A        double quoteChange = cursor.getDouble(StockQuoteFragment.COL_STOCK_CHANGE);%0A        holder.changeView.setText(fmt.format(quoteChange));%0A%0A        String changePercentPattern = %22#,##0.00%25%22;%0A        fmt.applyPattern(changePercentPattern);%0A        double quoteChangePercent = cursor.getDouble(StockQuoteFragment.COL_STOCK_CHANGE_PERCENT);%0A        holder.changePercentView.setText(%22(%22 + fmt.format(quoteChangePercent / 100) + %22)%22
 );%0A 

Use the ViewHolder pattern to save a reference to the Views