pound Pound
pound Pound

Reputation: 123

get value from listview not by position

maybe its easy but im getting hard to solve this. So i have item in list view, the following code is:

private List<Item> loadItem(){

    itemEntityList = new ArrayList<Item>();
    Item item1 = new Item();
    item1.setId(1);
    item1.setCategoryId(1);
    item1.setProductName("Blopress 8mg");
    item1.setPrice("10000");
    itemEntityList.add(item1);

    Item item2 = new Item();
    item2.setId(2);
    item2.setCategoryId(3);
    item2.setProductName("Blopress 16mg");
    item2.setPrice("12000");
    itemEntityList.add(item2);

    Item item3 = new Item();
    item3.setId(3);
    item3.setCategoryId(2);
    item3.setProductName("Celebrex 100mg");
    item3.setPrice("15000");
    itemEntityList.add(item3);

    Item item4 = new Item();
    item4.setId(4);
    item4.setCategoryId(1);
    item4.setProductName("Celebrex 200mg");
    item4.setPrice("17000");
    itemEntityList.add(item4);

return itemEntityList;
}

I want when i click on list view its toast message Id. I have try this but its toast position of listview. what i want its toast Id not position, any advice?

    listViewCatalog.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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


            Toast.makeText(CatalogActivity2.this, position+" _", Toast.LENGTH_LONG).show();


        }
    });

Upvotes: 0

Views: 109

Answers (1)

Milad Faridnia
Milad Faridnia

Reputation: 9477

There is a getItemId() method in your adapter. something like this:

@Override
public long getItemId(int position) {
    return mObjects.get(position).getId();
}

if you have implemented this method like this in your adapter. you can call it whenever you want and get id of your item like this:

yourAdapter.getItemId(position);

UPDATE as @pskink mentioned you can use the last parameter of onItemClick

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

which is id.

Upvotes: 3

Related Questions