0

I have a string that is being returned which is the location of an image as it would appear in the xml layout file so "@drawable/image".

My question is it possible to change the string in android:src="@drawable/image" from the code?

edit:

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

I'm using a SimpleAdapter with a CustomBinder:

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

Binder code:

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

}

I am getting an ID of the row item and building a string to get the name of the image, for example the first image is named u1.jpg which is in my drawable res folder.

Saml92
  • 91
  • 10
  • 2
    Possible duplicate of [Setting Android images from string value](http://stackoverflow.com/questions/4313007/setting-android-images-from-string-value) – Shaishav Oct 03 '16 at 18:44

1 Answers1

0

Are you looking for this ?

ImageView img = (ImageView) findViewById(R.id.img);
img.setImageResource(R.drawable.u1);
Varad Chemburkar
  • 458
  • 2
  • 6
  • 15
Nickan
  • 466
  • 5
  • 17