[英]Change ImageView Android:src with a string
我有一個返回的字符串,它是圖像的位置,因為它將出現在xml布局文件中,因此為“ @ drawable / image”。
我的問題是可以從代碼中更改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,該圖像位於我的drawable res文件夾中。
您在找這個嗎?
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageResource(R.drawable.u1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.