set image for each item in a list of items, during fragment on Create view

Rk R Bairi

I have a list of cards (cards_list.xml) , where each card has its own layout (card_in_list.xml).

The card has an 'ImageView' element, which am trying to change for each card in the list during fragment creation.

Here are the layouts and fragment onCreate code, thanks

cards_list.xml

 <ListView
        android:id="@+id/cards_listview"/>

card_in_list.xml

 <ImageView
        android:id="@+id/cardImage"
        android:src="@drawable/visa"/>

CardsListFragment.java

 public view OnCreateView(LayoutInflater inflater, ViewGroup container, 
                                             Bundle savedInstanceState) {

    // inflate cards list view 

    cardsListBinding binding = DataBindingUtil.inflate(inflater, 
                               R.layout.cards_list, container, false);
    binding.setSettingsService(this.settingsService);
    View view = binding.getRoot();

    // use adapter to set card layout to list of cards

    itemsList = (ListView) view.findViewById(R.id.cards_listview);
    itemsListAdapter = new ListViewBindingAdapter<>(getActivity(), 
                       R.layout.card_in_list, this, myService.getCards());

    // get image of each card item set a drawable to it

    int i = 0;
    for(Card c : myService().getCards()) {
      View cardListView = itemsListAdapter.getView(i,null,itemsList);
      ImageView iv = (ImageView)cardListView.findViewById(R.id.cardImage);
      iv.setImageResource(R.drawable.discover);
      i++;
    }

    itemsList.setAdapter(itemsListAdapter); 

    return view;
}
Ivan86

What you need is custom adapter for ListView inside of which you do what you need. This answer explains it nicely Custom Adapter for List View

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete items in List View populated by SQLite Database using a delete button in each list item in Android

From Dev

Delete items in List View populated by SQLite Database using a delete button in each list item in Android

From Dev

Create a Tree from a list of items, when each item contains a reference to its parent

From Dev

Multiple text items in List view item

From Dev

How to set an icon(picture) for each row in listview. List view items access through some folder in sdcard

From Dev

Swipe view with different actionbar items in each swipe fragment

From Dev

How to set View in Fragment without create class extends Fragment?

From Dev

image in the database is not getting changed for each list item

From Dev

How to set icons for a list view items on android

From Dev

iOS 7 : set an image(for background image or a View) dynamically during runtime?

From Dev

How to create circular list view of items in android?

From Dev

How to create circular list view of items in android?

From Dev

Create a new Object for each item in a list

From Dev

How to set focus on a List View item?

From Dev

Draw Circle in the background of Text View, over each image in Fragment

From Dev

Dynamically set image resource to a list-item

From Dev

populate a new list in fragment each time a list item in fragment drawer is clicked

From Dev

onCreateOptionsMenu called only for the selected fragment(item) inside view pager,( not called for left and right items of view pager)

From Dev

How to emit items from list with delay between each item?

From Dev

Taking a list of items and displaying a value from each item as a string

From Dev

How can I target each item in a list with collapsible items?

From Dev

How can I target each item in a list with collapsible items?

From Dev

How to emit items from list with delay between each item?

From Dev

making a list of items combining each item from several lists

From Dev

Accessibility reading showing items in a listview as list item + header view

From Dev

Maintaining background state of list view items after an item is deleted

From Dev

Custom item list view with Image View after the last character of textview

From Dev

List item is not visible in fragment

From Dev

How to set the Dynamic height to list view in fragment activity..?

Related Related

  1. 1

    Delete items in List View populated by SQLite Database using a delete button in each list item in Android

  2. 2

    Delete items in List View populated by SQLite Database using a delete button in each list item in Android

  3. 3

    Create a Tree from a list of items, when each item contains a reference to its parent

  4. 4

    Multiple text items in List view item

  5. 5

    How to set an icon(picture) for each row in listview. List view items access through some folder in sdcard

  6. 6

    Swipe view with different actionbar items in each swipe fragment

  7. 7

    How to set View in Fragment without create class extends Fragment?

  8. 8

    image in the database is not getting changed for each list item

  9. 9

    How to set icons for a list view items on android

  10. 10

    iOS 7 : set an image(for background image or a View) dynamically during runtime?

  11. 11

    How to create circular list view of items in android?

  12. 12

    How to create circular list view of items in android?

  13. 13

    Create a new Object for each item in a list

  14. 14

    How to set focus on a List View item?

  15. 15

    Draw Circle in the background of Text View, over each image in Fragment

  16. 16

    Dynamically set image resource to a list-item

  17. 17

    populate a new list in fragment each time a list item in fragment drawer is clicked

  18. 18

    onCreateOptionsMenu called only for the selected fragment(item) inside view pager,( not called for left and right items of view pager)

  19. 19

    How to emit items from list with delay between each item?

  20. 20

    Taking a list of items and displaying a value from each item as a string

  21. 21

    How can I target each item in a list with collapsible items?

  22. 22

    How can I target each item in a list with collapsible items?

  23. 23

    How to emit items from list with delay between each item?

  24. 24

    making a list of items combining each item from several lists

  25. 25

    Accessibility reading showing items in a listview as list item + header view

  26. 26

    Maintaining background state of list view items after an item is deleted

  27. 27

    Custom item list view with Image View after the last character of textview

  28. 28

    List item is not visible in fragment

  29. 29

    How to set the Dynamic height to list view in fragment activity..?

HotTag

Archive