0

In my app I'm using an xml file to store some data, and images are stored in the drawable folder. I would like to "link" these to, meaning that each object in the xml file to refer a certain image in the drawable folder. How could i acomplish this ? Thanks!

<contacts>
    <contact>
        <name>Joe</name>
        <picture>R.drawable.joe_pic</picture>
    </contact>
    <contact>
        <name>Dean</name>
        <picture>R.drawable.dean_pic</picture>
    </contact>
</contacts>
maephisto
  • 4,952
  • 11
  • 53
  • 73

1 Answers1

2

you can try with only saving the name like "joe_pic" and can get this way

private void showImage() {
    String uri = "drawable/icon";

    // int imageResource = R.drawable.icon;
    int imageResource = getResources().getIdentifier(uri, null, getPackageName());

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);
}

but if you have already added "R.drawable.joe_pic" complete can use String.split to get 3 rd string part

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36