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;
}
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.
Comments