I'm loading some Strings from the Asset folder and I store them in an ArrayList. On my Drawable folder I have icons for each continent but if the user deletes for example the folder "Africa" I don't want to load the corresponding icon from the drawable folder since the arraylist won't have the Africa string. Is there a way to do that?

Here is my function to get the arraylist with the names of the continents in the asset folder:

    private ArrayList<String> getContinents() {

    List<String> arrayList=new ArrayList<>() ;
    ArrayList<String> cont = new ArrayList<>();

    AssetManager am= getActivity().getAssets();
    try {
        String[] f = am.list("");

        arrayList =  Arrays.asList(f);
        int tam= arrayList.size();
        Collections.addAll(cont, f);
        cont.remove(tam-1);
        cont.remove(tam-2);
        for (String f1 : arrayList) {
            System.out.println( f1);
        }

    }
    catch (IOException e){

    }

    return  cont;
}

And here is how I get the Ids for each icon

Integer[] imgid={
R.drawable.africa,
R.drawable.europe,
R.drawable.oceania,
R.drawable.north_america,
R.drawable.south_america,
R.drawable.asia,

};

1 Answers

0
Daniel Andujar On Best Solutions

It should be something like this:

for (String _continent : continents )
{
    int resId = getResources().getIdentifier(_continent, "drawable", getPackageName());
    //this resId is the Resource Id of the drawable
    //use it like :
    imageView.setImageResource(resId);
}