Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove a string from arraylist of strings, ANDROID

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.

like image 247
Sandeep R Avatar asked Oct 10 '13 05:10

Sandeep R


People also ask

How remove all string from ArrayList?

textArray. clear(); It will clear whole ArrayList.

How do you remove multiple elements from an ArrayList in Java?

The List provides removeAll() method to remove all elements of a list that are part of the collection provided.


3 Answers

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");
like image 104
linakis Avatar answered Nov 09 '22 11:11

linakis


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.

like image 33
Hariharan Avatar answered Nov 09 '22 11:11

Hariharan


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);
like image 21
Pratik Dasa Avatar answered Nov 09 '22 12:11

Pratik Dasa