Question Detail

How to remove an element from String array in android ?

5 years ago Views 1194 Visit Post Reply

Hello,

I want to remove an element from my String Array in android.

In my Android Application, I am using an array to get data and print it in RecyclerView List but now I want to Delete any item from the list.

I have research on Google about this that how can remove an element from my String Array? and I got result but 

imagesPath = ArrayUtils.removeElement(imagesPath, imagesPath[position]);

ArrayUtils not found in android how can I access this class in android?

Please share any working example or suggestions for my Query.

Thanks


Thread Reply

Hemant Sharma

- 5 years ago

try this way: 

in this Program there is an issue it will remove all the same element from your Array

String[] socialPath={"Facebook","Google","Twitter","LinkedIN","Google"}

//...

@Override
public void onClick(DialogInterface dialog, int which) {
    socialPath=getRemovingElementArray(socialPath,socialPath[1]);
}

 

 String[] getRemovingElementArray(String[] chngeArray,String wantToRemove){
    String[] newArray = chngeArray;
    for (String s : newArray)
        System.out.print(s + ", ");
    newArray= removeArrayElement(newArray, wantToRemove);
    for (String s : newArray)
        System.out.print(s + ", ");
    return newArray;
}

 

String[] removeArrayElement(String[] array, String key){
    System.out.println("~~~~~~~~~~~~~~~REMOVING~~~~~~~~~~~~~~~~~~~~~~~key~~~"+key);
    int found = 0;
    for (String s : array)
    {
        if (s.equals(key))
            found++;
    }
    if (found == 0) return array;
    String[] temp = new String[array.length-found];
    int x = 0;
    for (String s : array)
    {
        if (s.equals(key))
            continue;
        temp[x++] = s;
    }
    return temp;
}

OUTPUT: 

socialPath={"Facebook","Twitter","LinkedIN"}

Hemant Sharma

- 5 years ago

try this way: 

in this Program there is an issue it will remove all the same element from your Array

String[] socialPath={"Facebook","Google","Twitter","LinkedIN","Google"}

//...

@Override
public void onClick(DialogInterface dialog, int which) {
    socialPath=getRemovingElementArray(socialPath,socialPath[1]);
}

 

 String[] getRemovingElementArray(String[] chngeArray,String wantToRemove){
    String[] newArray = chngeArray;
    for (String s : newArray)
        System.out.print(s + ", ");
    newArray= removeArrayElement(newArray, wantToRemove);
    for (String s : newArray)
        System.out.print(s + ", ");
    return newArray;
}

 

String[] removeArrayElement(String[] array, String key){
    System.out.println("~~~~~~~~~~~~~~~REMOVING~~~~~~~~~~~~~~~~~~~~~~~key~~~"+key);
    int found = 0;
    for (String s : array)
    {
        if (s.equals(key))
            found++;
    }
    if (found == 0) return array;
    String[] temp = new String[array.length-found];
    int x = 0;
    for (String s : array)
    {
        if (s.equals(key))
            continue;
        temp[x++] = s;
    }
    return temp;
}

OUTPUT: 

socialPath={"Facebook","Twitter","LinkedIN"}