我已经实施了一个名称,价格,数量等的自定义列表视图,
默认情况下,所有列表项中的数量均为1。
如果用户将列表项的数量之一从1更改为2,则必须在Sqlite数据库中更新更改的数量。
问题
我尝试使用addTextChangedListener
从用户输入中获取更新的数据,并在sqlite数据库中更新它。但addTextChangedListener
无效。
我无法更新数据库中的必填字段。
以下是我使用addTextChangedListener
的代码。
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_items_cart, null);
holder.tv_name = (TextView) rowView.findViewById(R.id.name_cart);
holder.tv_price = (TextView) rowView.findViewById(R.id.price_cart);
holder.image = (ImageView) rowView.findViewById(R.id.image_cart);
holder.tv_model = (TextView) rowView.findViewById(R.id.model_cart);
holder.tv_product = (TextView) rowView.findViewById(R.id.product_cart);
holder.delete = (Button) rowView.findViewById(R.id.delete);
holder.quantity = (EditText) rowView.findViewById(R.id.quantity);
rowView.setTag(holder);
}
else
holder = (Holder) rowView.getTag();
holder.tv_name.setText(list_name.get(position));
name = holder.tv_name.getText().toString();
holder.tv_price.setText(list_price.get(position));
price = holder.tv_price.getText().toString();
holder.tv_model.setText(list_model.get(position));
model = holder.tv_model.getText().toString();
holder.tv_product.setText(list_productid.get(position));
product = holder.tv_product.getText().toString();
holder.quantity.setText(quant_items.get(position));
quant = holder.quantity.getText().toString();
holder.image.setImageBitmap(loadImageFromStorage(list_images.get(position)));
image_new = holder.image.toString();
holder.quantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
value = s.toString().trim();
quant.replace(holder.quantity.getText().toString(), value);
updateTable();
}
});
final Holder finalHolder = holder;
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseHandler db = new DatabaseHandler(context);
//SQLiteDatabase db1 = db.getWritableDatabase();
// db.onUpgrade(db1,0,1);
deleteUser(finalHolder.tv_model.getText().toString());
ListView list = (ListView)parent;
cart_refresh.notifyDataSetChanged();
list.setAdapter(cart_refresh);
// db.deleteContact(new Cart(holder.tv_name.getText().toString(),holder.tv_price.getText().toString()
// ,holder.image.toString(),holder.tv_model.getText().toString()));
}
});
final View finalRowView = rowView;
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name_item = ((TextView) finalRowView.findViewById(R.id.name_cart)).getText().toString();
String price_item = ((TextView) finalRowView.findViewById(R.id.price_cart)).getText().toString();
model_item = ((TextView) finalRowView.findViewById(R.id.model_cart)).getText().toString();
Intent in = new Intent(context, AddCart_FullImage.class);
in.putExtra("model", model_item);
in.putExtra("name", name_item);
in.putExtra("price", price_item);
context.startActivity(in);
}
});
return rowView;
}
private Bitmap loadImageFromStorage(String path) {
try {
File f = new File(path, "");
f.canRead();
b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public void deleteUser(String userName)
{
final DatabaseHandler db = new DatabaseHandler(context);
SQLiteDatabase db1 = db.getWritableDatabase();
try
{
db1.delete("cart", "model = ?", new String[]{userName});
//cart_refresh.notifyDataSetChanged();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
db.close();
}
}
public void updateTable() {
final DatabaseHandler db = new DatabaseHandler(context);
SQLiteDatabase db1 = db.getWritableDatabase();
try {
db.updateContact(new Cart(name, price,image_new,model, product,value));
} catch (Exception e) {
}
}
任何帮助都会非常明显。
感谢。
答案 0 :(得分:0)
您最大的问题是,您正在使用字段而不是本地变量。
quant = holder.quantity.getText().toString();
每次调用quant
时都会更新getView()
@Override
public void afterTextChanged(Editable s) {
value = s.toString().trim();
quant.replace(holder.quantity.getText().toString(), value);
updateTable();
}
将使用最后绑定到数据的文本字段,而不是您添加监听器的文本字段。
您需要处理您使用的变量的范围,并为您的模型引入一致性。
我建议在不使用任何字段的情况下完全重写适配器。