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
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);
}
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]);
}
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());
}
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.