0

在应用程序的一个片段中,我将项目添加到数据库(Cart 类)并以下列方式(仅方法)捕获列表视图中的 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 的项目不是删除)。将不胜感激您的帮助!

4

1 回答 1

0

该问题很可能是由于不正确使用convertView. 您的代码假定convertView传递给getView匹配的position参数是不正确的假设。

为了验证这个假设,你可以用 替换(在两个 getViews 中)View v = convertView;View v = null;?(因此您不使用convertView)

如果这工作正常,那么我建议将 vi.inflate 之后的所有内容(在两个 getViews 中)放在周围的 if 语句之外。因此,您再次将标签设置为正确的值。

于 2012-11-06T08:46:20.730 回答