按id在setOnItemClickListener listview上获取项目

时间:2015-10-20 06:20:18

标签: android listview

我如何通过id而非按位置获取点击列表视图的项目详细信息。我的代码是:

listViewCatalog.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            //Category category = categoryAdapter.getItem(position);

            Intent productDetailsIntent = new Intent(getBaseContext(), ProductDetailsActivity.class);
            productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position );
            startActivity(productDetailsIntent);
        }
    });

代码工作,我可以在另一个类(ShoppingCartHelper.java)上查看我的产品的详细信息。但我想在该产品上使用id获取详细信息产品。任何建议的人?

1 个答案:

答案 0 :(得分:2)

得到Category id喜欢

listViewCatalog.setOnItemClickListener(new AdapterView.OnItemClickListener(){

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

Category item=(Category)parent.getItemAtPosition(position);

int categoryId=item.getCategoryId(); // get Category Id 

  }
});