2016-10-03 2 views
0

"@ drawable/image"와 같이 xml 레이아웃 파일에 나타날 이미지의 위치가 반환되는 문자열이 있습니다.변경 ImageView 안드로이드 : 문자열이있는 src

제 질문은 코드에서 android : src = "@ drawable/image"문자열을 변경할 수 있습니까?

편집 :

 <ImageView 
     android:id="@+id/imageuni" 
     android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:src="@drawable/no_image"/> <!--Change this part in code--> 

나는 CustomBinder와 SimpleAdapter을 사용하고 있습니다 :

 SimpleAdapter adapter = new SimpleAdapter(SearchActivity.this,leaderList, R.layout.search_entry, new String[] { 
       "rankId","name","location","image"}, new int[] {R.id.rankId,R.id.name,R.id.location,R.id.imageuni}); 
     adapter.setViewBinder(new CustomViewBinder()); 
     setListAdapter(adapter); 

바인더 코드 :

class CustomViewBinder implements SimpleAdapter.ViewBinder { 
    public boolean setViewValue(View view, Object inputData, String textRepresentation) { 
    int id = view.getId(); 
    String data = (String) inputData; 
    switch (id) { 
     case R.id.imageuni: 
      populateImage(view, data); 
      break; 

     case R.id.location: 
      populateLocation(view, data); 
      break; 

     case R.id.name: 
      populateName(view, data); 
      break; 

    } 
    return true; 
} 
public void populateImage(View view, String imageData) { 
    ImageView uniImage = (ImageView) view.findViewById(R.id.imageuni); 
    uniImage.setImageDrawable(Drawable.createFromPath(imageData)); 
} 

public void populateLocation(View view, String data) { 
    TextView locationTxt = (TextView) view.findViewById(R.id.location); 
    locationTxt.setText(data); 
} 

public void populateName(View view, String data) { 
    TextView dateTxt = (TextView) view.findViewById(R.id.name); 
    dateTxt.setText(data); 
} 

}

내가 ID를 얻고있다 행 항목 및 이미지의 이름을 얻기 위해 문자열을 작성합니다. 예를 들어 첫 번째 이미지의 이름은 u1.jpg이며 드로어 블 res 폴더에 있습니다.

+2

의 사용 가능한 복제 [문자열 값에서 안드로이드 이미지 설정] (http://stackoverflow.com/questions/4313007/setting-android-images-from-string-value) – Shaishav

답변

0

이걸 찾으십니까?

ImageView img = (ImageView) findViewById(R.id.img); 
img.setImageResource(R.drawable.u1); 
관련 문제