Orthmius
Orthmius

Reputation: 142

Deleting Indexes from an ArrayList<String>

I am trying to create a button that already has an index captures and just needs to delete from an ArrayList<String> and ArrayList<Integer>. The integer list deletes the index just fine but the string array do not. Code Below:

Globals:
ArrayList<String> QandItem = new ArrayList<String>();
ArrayList<Integer> Prices = new ArrayList<Integer>();
ArrayAdapter<String> left;
ArrayAdapter<Integer> right;

These are setup and filled in the on create method. I know they work correctly because I get output when viewing them on the device.

RemoveItem.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO
        if(i >= 0){
            QandItem.remove(i);
            Prices.remove(i);
            left.notifyDataSetChanged();
            right.notifyDataSetChanged();
            ItemList.setAdapter(left);
            PriceList.setAdapter(right);
        }
    }
});

Where i is the index I am trying to delete.

Upvotes: 0

Views: 197

Answers (2)

Spinner
Spinner

Reputation: 732

If i is an Integer rather than a primitive int, as Keppil asked, QandItem.remove(i) will be trying to remove an object that it doesn't hold. Try this:

QandItem.remove(i.intValue())

Upvotes: 1

Keppil
Keppil

Reputation: 46229

Since i is an Integer, you are trying to remove the object i and not the element at position i. Try using i.intValue() instead. See the difference between ArrayList.remove(int index) and ArrayList.remove(Object o).

Upvotes: 4

Related Questions