如何从 ListView 项更新 SQLite 行?
How do I update a SQLite row from a ListView item?
我有一个使用 sqlite 的应用程序,它存储有关硬件商店商品的信息并将它们显示在 ListView 中,此列表视图显示商品名称、价格、数量和供应商。每个列表项也有一个 Sell 按钮,当我单击该按钮时,它应该从该特定项目的数量中减去 1 并更新数据库,但是由于该按钮是在 CursorAdapter 中创建的,所以我不确定如何访问数据库和更新它。
这是我的 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);
}
}
在包含适配器引用的 activity 中,创建一个内部 class 类似于:
public class MyClickListener {
public void handleClick(Item item) {
// access your DB here, {item} is available if you need the data
}
}
然后当您创建适配器时
myAdapter = new InventoryCursorAdapter(context, cursor, new MyClickListener());
在您的适配器中保存对该点击侦听器的引用。
然后在adapter的BindView方法中(如果需要item数据更新数据库,通过点击监听传递)
sellButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Item item = myItemSet.get(position);
myListener.handleClick(item);
}
});
我有一个使用 sqlite 的应用程序,它存储有关硬件商店商品的信息并将它们显示在 ListView 中,此列表视图显示商品名称、价格、数量和供应商。每个列表项也有一个 Sell 按钮,当我单击该按钮时,它应该从该特定项目的数量中减去 1 并更新数据库,但是由于该按钮是在 CursorAdapter 中创建的,所以我不确定如何访问数据库和更新它。
这是我的 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);
}
}
在包含适配器引用的 activity 中,创建一个内部 class 类似于:
public class MyClickListener {
public void handleClick(Item item) {
// access your DB here, {item} is available if you need the data
}
}
然后当您创建适配器时
myAdapter = new InventoryCursorAdapter(context, cursor, new MyClickListener());
在您的适配器中保存对该点击侦听器的引用。
然后在adapter的BindView方法中(如果需要item数据更新数据库,通过点击监听传递)
sellButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Item item = myItemSet.get(position);
myListener.handleClick(item);
}
});