【问题标题】:Android - avoid multiple items on a listview and remove items properly from the data baseAndroid - 避免列表视图上的多个项目并从数据库中正确删除项目
【发布时间】:2012-11-06 05:20:07
【问题描述】:

在应用程序的一个片段中,我将项目添加到数据库(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;
                }
            }

购物车类(数据库):

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 没有被删除)。 将感谢您的帮助!

【问题讨论】:

    标签: java android listview items


    【解决方案1】:

    这个问题很可能是由于convertView 使用不当造成的。 您的代码假定传递给getViewconvertViewposition 参数相匹配,这是不正确的假设。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多