2014-02-28 2 views
-1

XML 파일 (tutor.xml)에 이미지 이름이 있습니다.
구문을 분석하고 이미지 이름을 가져 오려고 시도하고 있습니다. ImageView 여기를 클릭하십시오.
데이터 유형에 오류가 발생합니다 (이미지에는 적용되지 않음). 정수 여야합니다. android의 문자열을 통해 imageView에 이미지를 설정할 수 없습니다.

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(); 
    } 

는 제발 도와주세요 : 내 이미지를 설정하여야 방법 문자열을 통해하지 않으면 내가는 이미지 뷰 IV에 내 이미지를 설정하는 방법. 이 사항이 이미지 INT -ID를 반환합니다 안드로이드에서
감사

+0

setImageResource()는 int 형의 인수를 사용합니다. 대신 문자열을 전달하고 있습니다. int 값은 리소스 폴더의 드로어 블 리소스를 가리켜 야합니다. –

답변

2
R.drawable.xxx 

,

이미지 뷰 setImageResource (INT)에서

하지 않는 STRING -이 이미지 뷰의 내용으로 그릴 수를 설정합니다.

확인이 : http://developer.android.com/reference/android/widget/ImageView.html 귀하의 경우

,이 같은 int 배열 ... 모든 이미지 리소스 ID를 넣을 수 있습니다

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

예. 알고 있었지만이 문제에 대한 해결책은 없습니다. 156 이미지를 표시하기 때문에 배열에서 관리 할 수 ​​없습니다. –

+0

프로젝트의 모든 리소스 이미지는 상위 함수로로드 할 수 있으며 관리자가 필요하지 않습니다. –

0

이 코드를보십시오;

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]); 
} 
0

나를 위해 좋은 방법 (예 : http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/) 이미지 뷰 어댑터를 만들 수있을 것입니다

그런 다음 한 OnCreate 활동에 전화하고 활동의 분석 방법은 ArrayList에 채워

그리고 감사 어댑터로하고 (여기에 코드의 샘플 부) 귀하의 이미지 뷰에 이미지를 설정할 수 있습니다 ArrayList와 :

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()); 
} 
0

을 이 코드가 도움이 될 수 있습니다.

int id = getResId ("app_icon");

iv.setImageResource (id);

공공 INT의 getDrableId (문자열 변수 variableName) {

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