5

I created an arraylist of strings

List<String> textArray = new ArrayList<String>();

and then I added the strings(which I am getting from edittext) to textArray as follows

String text = editText1.getText().toString();
textArray.add(text);

Now I created a button and need to remove the string from the array when the button is clicked.But i dont know what to do. I know for arrays of bitmaps we clear a bitmap from array using recycle but Please suggest me how to remove or clear the string from arraylist.

braX
  • 11,506
  • 5
  • 20
  • 33
Sandeep R
  • 2,284
  • 3
  • 25
  • 51

4 Answers4

28

You can call one of:

to remove all entries

textArray.clear();

to remove from a specific position (e.g. first item)

textArray.remove(0);

to remove a specific string (that equals yours)

textArray.remove("myString");
linakis
  • 1,203
  • 10
  • 19
  • 2
    textArray.remove("myString"); not work with custom object ! –  Jun 25 '15 at 10:44
  • The original question was about strings. So yeah it will not work on custom objects. The reason is that your custom objects are not "equal". This is more of a language thing. Check http://tutorials.jenkov.com/java-collections/hashcode-equals.html – linakis Nov 14 '17 at 12:53
4

Try this..

Getting position from the textArray array list

int pos = textArray.indexOf(text);

and then remove from the string position

    textArray.remove(pos);

because we cannot directly remove the string. we can remove the string contains position.

Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

You can do it with more then one way as per your need.

textArray.clear(); 

It will clear whole ArrayList.

If you want to remove only some specifis data from index then,

textArray.remove(INDEX TO REMOVE);
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
0

Try This

String text = editText1.getText().toString();
textArray.add(text);

to remove

textArray.remove(text);
NARESH REDDY
  • 682
  • 4
  • 11