-1

I have a set of image names in my XML file (tutor.xml).
I am trying to parse it and get my image names and set to the ImageView here in my activity.
It gives datatype error (Not applicable for image) it must be an integer.

try {
    getAlphabet = getFromXML(MainActivity.this);
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
}
        String[] str = getAlphabet.split("\n");
        int lenAcAlp = str.length;
    String[] textFileLink = new String[lenAcAlp];
    String res = "R.drawable.";
    int key=0;
    while(key<lenAcAlp){
        textFileLink[key] = res + str[key];
    }
    ImageView iv = (ImageView)findViewById(R.id.imageView1);
    for(int i=0;i<lenAcAlp;i++){
        iv.setImageResource(textFileLink[i]);
    }
    }

private String getFromXML(MainActivity mainActivity)throws XmlPullParserException, IOException {
    StringBuffer stringBuffer = new StringBuffer();
        Resources res = mainActivity.getResources();
    XmlResourceParser xpp = res.getXml(R.xml.tutor);
    xpp.next();
    int eventType = xpp.getEventType();
    while(eventType != XmlPullParser.END_DOCUMENT){
        if(eventType == XmlPullParser.START_TAG){
            if(xpp.getName().equals("tutorappdata")){
            stringBuffer.append(xpp.getAttributeValue(null, "keyitem") +"\n");              }   
        }
        eventType = xpp.next(); 
    }
    return stringBuffer.toString();
    }

Please help me: how should I set my images to the ImageView iv, if not through string how shall set my images.
Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • The setImageResource() takes an argument of type int. You are passing a string instead. The int value should point to a drawable resource in the resource folder – Sankar V Feb 28 '14 at 10:31

4 Answers4

2
R.drawable.xxx

In Android this statement will return int -ID of image, not a STRING

In ImageView setImageResource(int) - Sets a drawable as the content of this ImageView.

Check this: http://developer.android.com/reference/android/widget/ImageView.html

In your case, please try this code

for(int i=0;i<lenAcAlp;i++){
        int id = getResources().getIdentifier(textFileLink[i], "drawable", getPackageName());
        iv.setImageResource(id);
}
Thanh Le
  • 763
  • 4
  • 13
0

you can put all image resource id as a int array...like this;

int[] images = new int[]{R.drawable.xx,R.drawable.xxx,R.drawable.xxxxx};
for(int i=0;i<images.length();i++){
  iv.setImageResource(images[i]);
 }
Kelvin Fu
  • 24
  • 2
0

For me a good way will be to create an ImageView Adapter (for instance: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/)

Then a parsing method in your activity which is called into OnCreate activity and which filled an ArrayList

Then thanks to your adapter and ArrayList you can set your image, in your imageView (here a sample part of code) :

ArrayList<YourObjectImage> _filePaths;

//Adapter constructor
public ImageViewAdapter(Activity activity, ArrayList<YourObjectImage> filePaths) {
        this._activity = activity;
        this._filePaths = filePaths;

    }

[Code here check my link above]

ImageView yourImage = (ImageView) imageView.findViewById(R.id.imageHere);

Image img = (Image) _filePaths.get(position); //here image is your object                 "image" in your case

if(yourImage  != null){
   yourImag.setImageResource(img.getId());
} 
Alex DG
  • 1,859
  • 2
  • 22
  • 34
0

Please this code may help you

int id=getResId("app_icon");

iv.setImageResource(id);

public int getDrableId(String variableName) {

    try {
         Class res = R.drawable.class;
        Field idField = res.getDeclaredField(variableName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    } 
}
sarwesh kumar
  • 95
  • 2
  • 6