如何得到单击的Andr​​oid项目的位置

如何得到单击的Andr​​oid项目的位置

本文介绍了如何得到单击的Andr​​oid项目的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好!
                   我想提出一个样品的购物车中,我需要获得项目的位置点击,并获得从购物cart..But在这里我只得到无论我有第一个项目的图像选择的图像的页面上显示的图像点击another..it总是显示列表的第一个图像...

Hello Everyone!! I am making a sample shopping cart in which i need to get the position of item clicked and get the image displayed on the page on selecting the image from the shopping cart..But here i am getting image of first item only no matter i have clicked another..it is always showing first image of the list...

下面是我的code为ProductAdapter.java

Here is my code for 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;
}}

和这里是我的购物车文件

And Here is my shoppingcart file

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

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);
        }
    });

}

}

code为ProductDetailsActivity.java

Code for 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();
        }
    });

}

PLZ家伙任何帮助将是非常美联社preciated。

Plz guys Any help will be highly appreciated.

感谢名单提前..

推荐答案

onItemClick INT位置给出在你给适配器数组/列表中单击项目的位置。
你也可以做getItemAtPosition();在你的列表视图,如果你没有你原来名单上的一个简单的句柄。

The int position in onItemClick gives the position of the clicked item in the array/list you gave to the adapter.You can also do getItemAtPosition(); on your listview, if you don't have an easy handle on your original list.

这篇关于如何得到单击的Andr​​oid项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:13