我无法理解如何让每个list_item的按钮只修改它所单击的行。我一直在查看使用ViewHolder类并实现setTag()/ getTag()的不同帖子方法,但我很难理解如何在我的代码中使用所有这些。我想要按钮做的是在一个TextView中增加数字的值,并在另一个TextView中减少另一个数字的值,然后更新数据库。到目前为止,当我单击按钮时,它会更改屏幕上任何list_item的值,而不是按钮所属的list_item,并且只能使用一次。我知道我可能只需通过传递当前视图告诉按钮哪个视图可以改变,但我只是感到困惑。 到目前为止,这是我的代码:
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 :(得分:0)
所以,我终于明白了。完全按照我需要的方式工作,我以为我之前就已经做到了,但经过更多的测试我发现了更多的错误。因此,如果在Cursor适配器中实现按钮点击时,任何人都需要帮助,这里就是。我使用了一个简单的ViewHolder模式,确保我的光标在单击按钮时位于正确的位置,并且解决它的最后一块...是我忘记在update()方法中指定WHERE子句-_- < / p>
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();
}
});
}
}