1

Suppose I have a list of item like String[] list={"item1","item2","item3",....,"itemN"}; I have corresponding images of each item having different formats(some in jpg,some in png etc.) and different names serially(as indices of list array) in drawable folder. I have to do some processing on the list array & each time it returns some particular & different indices of list. So,I have to show the corresponding images of those items indicated by returned indices no as list view in android studio. how can I do this. For example, If after processing I have indices no: { 3,7,1}, I have to show corresponding no of images for item3, item7 and item1 stored in drawable folder as listView. Again,indicated indices values may change depending on processing on it.

I want that here in the picture the sequence of displaying the images and texts will not be according to 'position',rather it will be according to my choice like in first row position of listview I will have the image of item3,in row position 2 ->image of item 7,in row position 3 -> image of item 1.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
suptagni
  • 61
  • 10

3 Answers3

0

Store the R value of image in integer array and you can post according to indices from that array

int _rImage[] ={R.drawable.img1,R.drawable.img2}
and-lab
  • 41
  • 2
0

If you want a ResourceId by name try something like

int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());

see How to get a resource id with a known resource name?

Community
  • 1
  • 1
Fusselchen
  • 382
  • 1
  • 4
  • 12
0

In your List Array processing class, define a Int array containing references to the images:

int imageRes[] = {R.drawable.image1, R.drawable.image2,...};

Now considering that only three indices are returned after processing: ex: indices[] ={1,5,6}; int imageArray[] - (Size can be three)

for(int i =0; i<3;i++){
imageArray[i] = imageRes[indices[i]];
}

Your ListView can contain one Image View. Create a custom adapter for the list view and pass imageArray as parameter.

Then you can set the Image for ImageView something like this:

imageView.setImageResource[imageArray[i]]
Sagar Das
  • 128
  • 9