Search code examples
androidsqliteandroid-contentprovider

How do I update a SQLite row from a ListView item?


I have an application that uses sqlite, it stores information about hardware store items and displays them in a ListView, this list view shows the name of the item, the price, the quantity, and the supplier. And each list item also has a Sell button and when I click the button it is supposed to subtract 1 from that specific item's quantity and update the database, but since the button is created in the CursorAdapter Im not sure how to access the database and update it.

This is my CursorAdapter:

package com.example.android.inventoryapp;

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

import com.example.android.inventoryapp.data.InventoryContract.InventoryEntry;


public class InventoryCursorAdapter extends CursorAdapter {


    public InventoryCursorAdapter(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 itemNameView = view.findViewById(R.id.item_name);
        TextView itemPriceView = view.findViewById(R.id.item_price);
        TextView itemQuantityView = view.findViewById(R.id.item_quantity);
        TextView itemSupplierView = view.findViewById(R.id.item_supplier);

        ImageView sellButton = view.findViewById(R.id.sell_button);

        int nameColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_NAME);
        int priceColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_PRICE);
        int quantityColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_QUANTITY);
        int supplierColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_SUPPLIER);

        int quantity = cursor.getInt(quantityColumnIndex);

        String name = cursor.getString(nameColumnIndex);
        String price = String.valueOf(cursor.getInt(priceColumnIndex)) + context.getString(R.string.currency_symbol);
        String quantityStr = String.valueOf(quantity);
        String supplier = cursor.getString(supplierColumnIndex);

        itemNameView.setText(name);
        itemPriceView.setText(price);
        itemQuantityView.setText(quantityStr);
        itemSupplierView.setText(supplier);

    }

}

Solution

  • In your activity that holds the adapter reference, create an inner class something like:

      public class MyClickListener {
        public void handleClick(Item item) {
          // access your DB here, {item} is available if you need the data
        }
      }
    

    then when you create your adapter

    myAdapter = new InventoryCursorAdapter(context, cursor, new MyClickListener());
    

    save the reference to that click listener in your adapter.

    then in the adapter's BindView method (if you need the item data to update the database, pass it through the click listener)

    sellButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          Item item = myItemSet.get(position);
          myListener.handleClick(item);
       }
    });