0

大家好!!我正在制作一个示例购物车,在该购物车中,我需要获取点击的商品位置,并在从购物车中选择图片时获取页面上显示的图片。但无论我有没有,这里我只获取第一个商品的图片单击另一个..它总是显示列表的第一个图像...

这是我的 ProductAdapter.java 代码

public class ProductAdapter extends BaseAdapter {

private List<Product> mProductList;
private LayoutInflater mInflater;
private boolean mShowQuantity;

public ProductAdapter(List<Product> list, LayoutInflater inflater, boolean showQuantity) {
    mProductList = list;
    mInflater = inflater;
    mShowQuantity = showQuantity;
}

@Override
public int getCount() {
    return mProductList.size();
}

@Override
public Object getItem(int position) {
    return mProductList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewItem item;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item, null);
        item = new ViewItem();

        item.productImageView = (ImageView) convertView
                .findViewById(R.id.ImageViewItem);

        item.productTitle = (TextView) convertView
                .findViewById(R.id.TextViewItem);

        item.productQuantity = (TextView) convertView
                .findViewById(R.id.textViewQuantity);



        convertView.setTag(item);
    } else {
        item = (ViewItem) convertView.getTag();
    }


     Product curProduct = mProductList.get(position);


    item.productImageView.setImageDrawable(curProduct.productImage);
    item.productTitle.setText(curProduct.title);

    // Show the quantity in the cart or not
    if (mShowQuantity) {
        item.productQuantity.setText("Quantity: "
                + ShoppingCartHelper.getProductQuantity(curProduct));
    } else {
        // Hid the view
        item.productQuantity.setVisibility(View.GONE);
    }

    return convertView;
}

private class ViewItem {
    ImageView productImageView;
    TextView productTitle;
    TextView productQuantity;
}}

这是我的购物车文件

public class ShoppingCartActivity extends Activity {

private List<Product> mCartList;
private ProductAdapter mProductAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shoppingcart);

    mCartList = ShoppingCartHelper.getCartList();

    // Make sure to clear the selections
    for (int i = 0; i < mCartList.size(); i++) {
        mCartList.get(i).selected = false;
    }

    // Create the list
    final ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
    mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(),
            true);
    listViewCatalog.setAdapter(mProductAdapter);

    listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Intent productDetailsIntent = new Intent(getBaseContext(),
                    ProductDetailsActivity.class);
            productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX,
                    position);
            startActivity(productDetailsIntent);
        }
    });

}

@Override
protected void onResume() {
    super.onResume();

    // Refresh the data
    if (mProductAdapter != null) {
        mProductAdapter.notifyDataSetChanged();
    }

    double subTotal = 0;
    for (Product p : mCartList) {
        int quantity = ShoppingCartHelper.getProductQuantity(p);
        subTotal += p.price * quantity;
    }

    TextView productPriceTextView = (TextView) findViewById(R.id.TextViewSubtotal);
    productPriceTextView.setText("Subtotal: $" + subTotal);
}

}

ProductActivity.java

public class CatalogActivity extends Activity {

private List<Product> mProductList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.catalog);

    // Obtain a reference to the product catalog
    mProductList = ShoppingCartHelper.getCatalog(getResources());

    // Create the list
    ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
    listViewCatalog.setAdapter(new ProductAdapter(mProductList, getLayoutInflater(), false));

    listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Intent productDetailsIntent = new Intent(getBaseContext(),ProductDetailsActivity.class);
            productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
            startActivity(productDetailsIntent);
        }
    });

    Button viewShoppingCart = (Button) findViewById(R.id.ButtonViewCart);
    viewShoppingCart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class);
            startActivity(viewShoppingCartIntent);
        }
    });

}

}

ProductDetailsActivity.java 的代码

public class ProductDetailsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.productdetails);

    List<Product> catalog = ShoppingCartHelper.getCatalog(getResources());

    int productIndex = getIntent().getExtras().getInt(
            ShoppingCartHelper.PRODUCT_INDEX);
    final Product selectedProduct = catalog.get(productIndex);

    // Set the proper image and text
    ImageView productImageView = (ImageView) findViewById(R.id.ImageViewProduct);
    productImageView.setImageDrawable(selectedProduct.productImage);
    TextView productTitleTextView = (TextView) findViewById(R.id.TextViewProductTitle);
    productTitleTextView.setText(selectedProduct.title);
    TextView productDetailsTextView = (TextView) findViewById(R.id.TextViewProductDetails);
    productDetailsTextView.setText(selectedProduct.description);

    TextView productPriceTextView = (TextView) findViewById(R.id.TextViewProductPrice);
    productPriceTextView.setText("$" + selectedProduct.price);

    // Update the current quantity in the cart
    TextView textViewCurrentQuantity = (TextView) findViewById(R.id.textViewCurrentlyInCart);
    textViewCurrentQuantity.setText("Currently in Cart: "
            + ShoppingCartHelper.getProductQuantity(selectedProduct));

    // Save a reference to the quantity edit text
    final EditText editTextQuantity = (EditText) findViewById(R.id.editTextQuantity);

    Button addToCartButton = (Button) findViewById(R.id.ButtonAddToCart);
    addToCartButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // Check to see that a valid quantity was entered
            int quantity = 0;
            try {
                quantity = Integer.parseInt(editTextQuantity.getText()
                        .toString());

                if (quantity < 0) {
                    Toast.makeText(getBaseContext(),
                            "Please enter a quantity of 0 or higher",
                            Toast.LENGTH_SHORT).show();
                    return;
                }

            } catch (Exception e) {
                Toast.makeText(getBaseContext(),
                        "Please enter a numeric quantity",
                        Toast.LENGTH_SHORT).show();

                return;
            }

            // If we make it here, a valid quantity was entered
            ShoppingCartHelper.setQuantity(selectedProduct, quantity);

            // Close the activity
            finish();
        }
    });

}

请大家帮忙,我们将不胜感激。

提前谢谢..

4

3 回答 3

2

int positioninonItemClick给出了您提供给适配器的数组/列表中单击项目的位置。你也可以做 getItemAtPosition(); 在您的列表视图上,如果您对原始列表没有简单的处理。

于 2013-03-13T08:36:12.093 回答
0

将此代码添加到您的项目中:

 mProductList.setOnItemClickListener(new OnItemClickListener() 

        {
        public void onItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                

            int  h = parent.getPositionForView(v);



            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected" + h, 
                    Toast.LENGTH_SHORT).show();
        }
    });        
于 2013-03-13T08:42:40.953 回答
0

只是 geussig 你在读取索引值的地方有一个有问题的代码。以下是编写和读取 int extra 的示例代码:

输入 int 值:

productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX,
                    position);

以下代码应用于在另一个中读取此值Activity

int index = getIntent().getExtras().getInt(ShoppingCartHelper.PRODUCT_INDEX);

希望能帮助到你...

于 2013-03-13T08:51:50.480 回答