Q:

SetOnClickListener() to Button in list_item using a CursorAdapter

I am having trouble understanding how to have the button of each list_item modify only the row that it is clicked in. I have been looking through different posts that have mentioned using a ViewHolder class and implementing the setTag()/getTag() methods, but i am having trouble understanding exactly how to use all this in my code.  What i want the button to do is increase the value of the number in one TextView and decrease the value of another number in a different TextView, then update the database. So far when i click the button, it changes the values of any list_item on the screen instead of the list_item the button belongs to and only works once.  I know i probably just need to tell the button which view to change by passing the current view but i am just getting confused.
 Here is my code so far:
package com.example.ms.inventory_app;

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

import com.example.ms.inventory_app.data.ProductContract.ProductEntry;

public class ProductCursorAdapter extends CursorAdapter {
    private String mSold, mQuantity;
    private TextView mQuantityTextView, mSoldTextView;
    private Button mButton;

    public ProductCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView nameTextView = (TextView) view.findViewById(R.id.product_name);
        String name = cursor.getString(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_NAME));
        nameTextView.setText(name);

        TextView priceTextView = (TextView) view.findViewById(R.id.product_price);
        String price = Integer.toString(cursor.getInt(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_PRICE)));
        priceTextView.setText(price);

        mQuantityTextView = (TextView) view.findViewById(R.id.product_quantity);
        mQuantity = Integer.toString(cursor.getInt(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_QUANTITY)));
        mQuantityTextView.setText(mQuantity);

        mSoldTextView = (TextView) view.findViewById(R.id.product_sold);
        mSold = Integer.toString(cursor.getInt(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_SOLD)));
        mSoldTextView.setText(mSold);

        mButton = (Button) view.findViewById(R.id.sale_button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int currentSold = Integer.parseInt(mSold);
                int currentQuantity = Integer.parseInt(mQuantity);
                currentSold+=1;
                currentQuantity-=1;
                mSoldTextView.setText(Integer.toString(currentSold));
                mQuantityTextView.setText(Integer.toString(currentQuantity));
            }
        });
    }
}

Thanks in advance for any help

A:

So, i finally figured it out. Working exactly as i need it to, i thought i had it right before but upon more testing i found more bugs. So, in case anyone happens to need help with their button clicks when implemented within a Cursor adapter, here it is. I used a simple ViewHolder pattern, made sure my cursor was in the correct position when the button was clicked, and the final piece that solved it...was me forgetting to specify a WHERE clause in my update() method -_-   
package com.example.ms.inventory_app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ms.inventory_app.data.ProductContract.ProductEntry;
import com.example.ms.inventory_app.data.ProductDbHelper;

public class ProductCursorAdapter extends CursorAdapter {

    private LayoutInflater mInflater;
    //private Holder mHolder;

    static private class Holder {
        TextView mSoldTextView;
        TextView mQuantityTextView;
        Button mSaleButton;

        public Holder(View view) {
            mSoldTextView = (TextView) view.findViewById(R.id.product_sold);
            mQuantityTextView = (TextView) view.findViewById(R.id.product_quantity);
            mSaleButton = (Button) view.findViewById(R.id.sale_button);
        }
    }

    public ProductCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.list_item, parent, false);
        Holder holder = new Holder(view);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        Log.d("Position " + cursor.getPosition() + ":", " bindView() has been called.");

        final Holder holder = (Holder) view.getTag();

        TextView nameTextView = (TextView) view.findViewById(R.id.product_name);
        String name = cursor.getString(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_NAME));
        nameTextView.setText(name);

        TextView priceTextView = (TextView) view.findViewById(R.id.product_price);
        String price = Integer.toString(cursor.getInt(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_PRICE)));
        priceTextView.setText(price);

        int quantity = cursor.getInt(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_QUANTITY));
        String quantityText = Integer.toString(quantity);
        holder.mQuantityTextView.setText(quantityText);

        int sold = cursor.getInt(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_SOLD));
        String soldText = Integer.toString(sold);
        holder.mSoldTextView.setText(soldText);

        final int position = cursor.getPosition();
        holder.mSaleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cursor.moveToPosition(position);

                int currentSold = Integer.parseInt(holder.mSoldTextView.getText().toString());
                int currentQuantity = Integer.parseInt(holder.mQuantityTextView.getText().toString());

                if (currentQuantity == 0) {
                    Toast.makeText(view.getContext(), "No product available \n Please place order", Toast.LENGTH_SHORT).show();
                    return;
                }

                currentSold+=1;
                currentQuantity-=1;
                holder.mSoldTextView.setText(String.valueOf(currentSold));
                holder.mQuantityTextView.setText(String.valueOf(currentQuantity));

                ProductDbHelper dbHelper = new ProductDbHelper(view.getContext());
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(ProductEntry.COLUMN_PRODUCT_SOLD, currentSold);
                values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, currentQuantity);

                long id = cursor.getLong(cursor.getColumnIndex(ProductEntry._ID));
                db.update(ProductEntry.TABLE_NAME, values, "_id=" + id, null);
                db.close();
            }
        });
    }
}

