在应用程序的一个片段中,我将项添加到数据库(Cart类)并以下列方式捕获listview中的editText(仅getView()
方法):
public View getView(int position, View convertView,final ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.layout_product, null);
final Button addToCart = (Button) v.findViewById(R.id.button_addToCart);
addToCart.setTag(position);
addToCart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Fetch product id
int position = (Integer) v.getTag();
// Fetch amount of product
LinearLayout rowLayout = (LinearLayout) v.getParent();
EditText editText_amount = (EditText) rowLayout
.findViewById(R.id.editText_amount);
try {
int amount = Integer.parseInt(editText_amount.getText()
.toString());
String k = Integer.toString(amount);
Toast.makeText(getActivity(),k,Toast.LENGTH_SHORT).show();
Cart.AddToCart(products.get(position), amount);
} catch (NumberFormatException e) {
return;
}
}
Cart类(数据库):
public class Cart {
static HashMap<Integer, JSONObject> products = new HashMap<Integer, JSONObject>();
public static void AddToCart(JSONObject product, int amount) {
int product_id;
try {
product_id = product.getInt("product_id");
product.put("product_id", amount);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
products.put(product_id, product);
}
public static void ExcludeFromCart(JSONObject product, int amount) {
int product_id;
try {
product_id = product.getInt("product_id");
product.put("product_id", amount);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
products.remove(product_id);
}
public static JSONObject[] GetProducts()
{
return products.values().toArray(new JSONObject[products.size()]);
}
}
以及填充和处理Cart类中添加项目的列表视图的片段(仅getView()
方法):
public View getView(int position, View convertView,
final ViewGroup parent) {
ViewGroup p=parent;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.layout_cart, null);
Button remove = (Button) v.findViewById(R.id.remove);
remove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Fetch product id
int position = (Integer) v.getTag();
String k = Integer.toString(position);
Toast.makeText(getActivity(),k,Toast.LENGTH_SHORT).show();
// Fetch amount of product
LinearLayout rowLayout = (LinearLayout) v.getParent();
EditText editText_amount = (EditText) rowLayout
.findViewById(R.id.editText_amount_cart);
try {
int amount = Integer.parseInt(editText_amount.getText()
.toString());
Cart.ExcludeFromCart(products.get(position), amount);
v.invalidate();
notifyDataSetChanged();
} catch (NumberFormatException e) {
return;
}
}
});
问题在于偶尔(并非总是如此,这使得它更奇怪)我可以向购物车添加多个商品(当然我想避免这种情况)当我按下删除按钮时,没有任何反应(项目的listView未被删除)。 非常感谢您的帮助!
答案 0 :(得分:0)
问题很可能是由于convertView
使用不当造成的。
您的代码假定传递给convertView
的{{1}}与getView
参数匹配,这是一个不正确的假设。
要验证此假设,您可以使用position
替换(在两个getView中)View v = convertView;
吗? (因此你不使用convertView)
如果这样可以正常工作,那么我建议将vi.inflate之后的所有内容(在两个getViews中)放在周围的if语句之外。因此,您将再次将标记设置为正确的值。