2017-07-26 24 views
2

私は、sqliteを使用するアプリケーションを持っています。ハードウェアストアアイテムに関する情報を格納し、ListViewに表示します。このリストビューは、アイテムの名前、価格、数量、およびサプライヤを表示します。各リストアイテムには売りボタンもあり、ボタンをクリックすると、そのアイテムの数量から1を減算してデータベースを更新するはずですが、ボタンがCursorAdapterで作成されているため、データベースへのアクセス方法や更新方法がわかりませんそれ。ListViewアイテムからSQLite行を更新するにはどうすればよいですか?

これは私の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); 

    } 

} 

答えて

1

、のような内部クラスを作成する何か:

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()); 

そのクリックリスナーへの参照をアダプタに保存します。その後、アダプタのBindView方法で

(データベースを更新するために、アイテムデータが必要な場合は、クリックリスナーを介して、それを渡す)

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

あなたはGENIUSです!ありがとうございます –

+0

私の問題は、最初にクリックしたListItemだけを変更できることです。私は、sellButtonがどのListItemに属しているかを知りません。 –

+0

具体的な方法が必要な場合は、handleClick()で追加情報を渡す必要があります。情報を識別することができますが、handleClick()内の各特定のカーソルのデータを渡すので、必要なものすべてを持つ必要があるようです。 – Chad

関連する問題