How to dynamically set imageView source for a List View from Custom Adapter?

Major Major

I'm trying to set an imageView source based on a string within a custom ArrayAdapter, but I'm unable to get it to work. I know the object rec_gift is coming through correctly and the icon_string variable get's the correct name, but the setImageResource call isn't working.

public class MySimpleArrayAdapter extends ArrayAdapter<Gift> {
  private final Context context;
  private List<Gift> giftz2;

  public MySimpleArrayAdapter(Context context, List<Gift> giftx) {
        super(context, R.layout.listview_rowlayout, giftx);
    this.context = context;
        this.giftz2 = giftx;
      }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.listview_rowlayout, parent, false);

    ImageView iconz = (ImageView) rowView.findViewById(R.id.icon);

    Gift rec_gift = (Gift) getItem(position);

    String icon_string = "R.drawable." + rec_gift.photo_key;

    iconz.setImageResource(getImageId(icon_string));

    return rowView;
  }

  public int getImageId(String imageName) {
        return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
    }
} 

In the above code, I want to set the imageView based on the icon_string variable so each row in the listView will get the correct image but it's not making a change at all.

Devrim

Implement getItem method of your custom adapter:

@Override
public Gift getItem(int position) {
    if(this.giftz2 != null) {
        this.giftz2.get(position);
    } else {
        return null;
    }
}

And change this:

public int getImageId(String imageName) {
    return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}

To this:

public int getImageId(String imageName) {
    return context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
}

And call method:

String icon_string = String.valueOf(rec_gift.photo_key);
iconz.setImageResource(getImageId(icon_string));

BTW: Try to reuse your views at your adapters getView method. If you continue using your implementation, you are going to get memory errors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android: How to set the ImageView src in a list dynamically

From Dev

Dynamically add items to list view using custom adapter for Android app

From Dev

Android getText from dynamically created adapter list view

From Dev

Passing data for list view from Asynctask to a custom list adapter class

From Dev

Android dynamically set ImageView in a list

From Dev

How to get data from custom list view adapter and pass to intent put extra?

From Dev

How to dynamically take a imageview from an ImageView List and place that imageview to an imageview variable

From Dev

how to set rgb color dynamically on list view?

From Dev

How to dynamically add ImageView to layout using Adapter

From Dev

Dynamically change an ImageView source on a custom action bar

From Dev

Custom text view adapter not working on list view

From Dev

Custom text view adapter not working on list view

From Dev

Add Images in ImageView of Listview dynamically via Custom Adapter

From Dev

set image dynamically to the custom imageview class android

From Dev

how to set a custom list view into a dialog box

From Dev

Dynamically add textviews to custom adapter from ArrayList

From Dev

Cannot resolve layout in adapter for custom list view

From Dev

android: how can i read Jsonobject from custom list adapter

From Dev

Footer button in listview, how to get value from custom list adapter

From Dev

How to separate ImageView from adapter of ListView

From Dev

How to pass multiple array values in a Custom Adapter class for Custom List View?

From Dev

Cant set list to custom adapter in fragment

From Dev

Android Set ImageView Source from Java

From Dev

Android list view using custom adapter and view holder

From Dev

Set ImageView visible/invisible from activity's adapter

From Dev

How to make custom view ( imageview + progressbar)?

From Dev

How to make custom view ( imageview + progressbar)?

From Dev

ListView Custom adapter lose imageView

From Dev

ImageView crashing custom adapter at getView()

Related Related

  1. 1

    Android: How to set the ImageView src in a list dynamically

  2. 2

    Dynamically add items to list view using custom adapter for Android app

  3. 3

    Android getText from dynamically created adapter list view

  4. 4

    Passing data for list view from Asynctask to a custom list adapter class

  5. 5

    Android dynamically set ImageView in a list

  6. 6

    How to get data from custom list view adapter and pass to intent put extra?

  7. 7

    How to dynamically take a imageview from an ImageView List and place that imageview to an imageview variable

  8. 8

    how to set rgb color dynamically on list view?

  9. 9

    How to dynamically add ImageView to layout using Adapter

  10. 10

    Dynamically change an ImageView source on a custom action bar

  11. 11

    Custom text view adapter not working on list view

  12. 12

    Custom text view adapter not working on list view

  13. 13

    Add Images in ImageView of Listview dynamically via Custom Adapter

  14. 14

    set image dynamically to the custom imageview class android

  15. 15

    how to set a custom list view into a dialog box

  16. 16

    Dynamically add textviews to custom adapter from ArrayList

  17. 17

    Cannot resolve layout in adapter for custom list view

  18. 18

    android: how can i read Jsonobject from custom list adapter

  19. 19

    Footer button in listview, how to get value from custom list adapter

  20. 20

    How to separate ImageView from adapter of ListView

  21. 21

    How to pass multiple array values in a Custom Adapter class for Custom List View?

  22. 22

    Cant set list to custom adapter in fragment

  23. 23

    Android Set ImageView Source from Java

  24. 24

    Android list view using custom adapter and view holder

  25. 25

    Set ImageView visible/invisible from activity's adapter

  26. 26

    How to make custom view ( imageview + progressbar)?

  27. 27

    How to make custom view ( imageview + progressbar)?

  28. 28

    ListView Custom adapter lose imageView

  29. 29

    ImageView crashing custom adapter at getView()

HotTag

Archive