2017-02-12 32 views
0

我无法理解如何让每个list_item的按钮只修改它被单击的行。我一直在寻找通过使用ViewHolder类的不同职位并实现setTag()/ getTag()方法,但我无法准确理解如何在代码中使用所有这些。我想要的按钮是增加一个TextView中数字的值,并减少另一个TextView中另一个数字的值,然后更新数据库。到目前为止,当我点击按钮时,它会更改屏幕上任何list_item的值,而不是该按钮所属的list_item,并且只能使用一次。我知道我可能只需要通过传递当前视图来告诉按钮哪个视图改变,但我只是感到困惑。 这里是我到目前为止的代码:SetOnClickListener()to list_item中的按钮使用CursorAdapter

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

预先感谢任何帮助

回答

0

所以,我终于想通了。正如我所需要的那样工作,我认为我之前有过它,但经过更多的测试,我发现了更多的错误。所以,如果任何人在Cursor适配器中实现时碰巧需要按钮点击帮助,那么就是这样。我使用了一个简单的ViewHolder模式,确保当点击按钮时我的光标处于正确的位置,并且解决它的最后一块......我忘记在update()方法中指定WHERE子句-_-

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(); 
      } 
     }); 
    } 
} 
相关问题