0

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;
}
Rk R Bairi
  • 1,289
  • 7
  • 15
  • 39

1 Answers1

1

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

Ivan86
  • 356
  • 1
  • 14