user3024235
user3024235

Reputation:

List.Remove always removes last item for ListView

When i try to remove a specific item from a list View:

        buyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            tempToken -= selPerk.cost;
            plrPerks.add(selPerk);
            String tokStr = String.valueOf(tempToken);
            tkn.setText(tokStr);
            shopItems.remove(selPerk);
            selPerk = new Perk();
            perkDialog.dismiss();
        }
    });

It always seems to remove the last item. This is where i open the dialog:

perks.setClickable(true);
    perks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            Perk perk = (Perk) perks.getItemAtPosition(position);
            showItem(perk);
        }
    });
}

This is the show Item function:

public void showItem(Perk perk) {
    if (tempToken >= perk.cost) {
        selPerk = perk;

How do i remove a specific item from a list and list view respectively?

Thanks for your time :)

Upvotes: 1

Views: 1044

Answers (3)

amit singh
amit singh

Reputation: 1422

In "setOnItemClickListener" listener, you are getting the perk object. So you can remove that object from your list like this-

shopItems.remove(perk);

and then you can call-

your_adapter.notifyDataSetChanged();

to refresh your listview.

Upvotes: 1

user3024235
user3024235

Reputation:

I fixed it. Whenever i removed an item i had to do it like so:

shopItems.remove(selPerk);
perk_adapter.notifyDataSetChanged();

So i had to notify my listeview adapter that i removed an item.

Upvotes: 0

Sean McCullough
Sean McCullough

Reputation: 11

To remove a specific item from a list view, your can call removeView(View toBeRemoved) if you have a reference to the view you wish to remove. If you have the index, you can call removeView(int index).

http://developer.android.com/reference/android/widget/ListView.html

You can remove a specific item from a list in the same way, using remove(Object item) or remove(int index).

http://docs.oracle.com/javase/7/docs/api/java/util/List.html

Hope this helps!

Upvotes: 0

Related Questions