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;

import com.seandbeach.stockticker.data.StockContract.StockEntry;

/**
 * {@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) {
        // get row indices for our cursor
        int idx_symbol = cursor.getColumnIndex(StockEntry.COLUMN_SYMBOL);
        int idx_name = cursor.getColumnIndex(StockEntry.COLUMN_NAME);
        int idx_price = cursor.getColumnIndex(StockEntry.COLUMN_LAST_TRADE_PRICE);
        int idx_change = cursor.getColumnIndex(StockEntry.COLUMN_CHANGE);
        int idx_change_percent = cursor.getColumnIndex(StockEntry.COLUMN_PERCENT_CHANGE);

        String name = cursor.getString(idx_name);

        return cursor.getString(idx_symbol)
                + (name != null && !name.isEmpty() && !name.equals("null") ? (" (" + name + "): ") : ": ")
                + cursor.getDouble(idx_price)
                + " ("
                    + cursor.getDouble(idx_change)
                    + ", " + cursor.getDouble(idx_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));
    }
}

@@ -261,74 +261,8 @@
 w;%0A%0A
-import com.seandbeach.stockticker.data.StockContract.StockEntry;%0A%0A
 /**%0A
@@ -561,33 +561,25 @@
     %7D%0A%0A    /
-*%0A       
+/
  This is por
@@ -651,24 +651,16 @@
 r to the
-%0A       
  string.
@@ -660,24 +660,16 @@
 string.%0A
-     */%0A
     priv
@@ -735,277 +735,84 @@
     
-// get row indices for our cursor%0A        int idx_symbol = cursor.getColumnIndex(StockEntry.COLUMN_SYMBOL);%0A        int idx_name = cursor.getColumnIndex(StockEntry.COLUMN_NAME);%0A        int idx_price = cursor.getColumnIndex(StockEntry.COLUMN_LAST_TRADE_PRIC
+String name = cursor.getString(StockQuoteFragment.COL_STOCK_NAM
 E);%0A
+%0A
         
 int 
@@ -811,258 +811,67 @@
     
-int idx_change = cursor.getColumnIndex(StockEntry.COLUMN_CHANGE);%0A        int idx_change_percent = cursor.getColumnIndex(StockEntry.COLUMN_PERCENT_CHANGE);%0A%0A        String name = cursor.getString(idx_name);%0A%0A        return cursor.getString(idx_symbol
+return cursor.getString(StockQuoteFragment.COL_STOCK_SYMBOL
 )%0A  
@@ -1014,17 +1014,42 @@
 ble(
-idx_price
+StockQuoteFragment.COL_STOCK_PRICE
 )%0A  
@@ -1108,26 +1108,51 @@
 tDouble(
-idx_change
+StockQuoteFragment.COL_STOCK_CHANGE
 )%0A      
@@ -1195,26 +1195,51 @@
 ble(
-idx_change_percent
+StockQuoteFragment.COL_STOCK_CHANGE_PERCENT
 ) + 
@@ -1277,25 +1277,17 @@
 %7D%0A%0A    /
-*%0A       
+/
  Remembe
@@ -1327,24 +1327,16 @@
 needed.%0A
-     */%0A
     @Ove
@@ -1554,17 +1554,9 @@
    /
-*%0A       
+/
  Thi
@@ -1624,16 +1624,8 @@
 or.%0A
-     */%0A
     
@@ -1842,17 +1842,16 @@
 inding.%0A
-%0A
         

Use DB projection in StockAdapter