void updateCustomer(@NonNull Customer customer) { mCustomerSourceList.clear(); CustomerSource[] customerSources = new CustomerSource[customer.getSources().size()]; addCustomerSourceIfSupported(customer.getSources().toArray(customerSources)); String sourceId = customer.getDefaultSource(); if (sourceId == null) { updateSelectedIndex(NO_SELECTION); } else { setSelectedSource(sourceId); } notifyDataSetChanged(); }
private void createListFromCustomerSources() { setCommunicatingProgress(false); if (mCustomer == null) { return; } List<CustomerSource> customerSourceList = mCustomer.getSources(); if (!mRecyclerViewUpdated) { mMaskedCardAdapter = new MaskedCardAdapter(customerSourceList); // init the RecyclerView RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setHasFixedSize(false); mRecyclerView.setLayoutManager(linearLayoutManager); mRecyclerView.setAdapter(mMaskedCardAdapter); mRecyclerViewUpdated = true; } else { mMaskedCardAdapter.setCustomerSourceList(customerSourceList); } String defaultSource = mCustomer.getDefaultSource(); if (!TextUtils.isEmpty(defaultSource)) { mMaskedCardAdapter.setSelectedSource(defaultSource); } mMaskedCardAdapter.notifyDataSetChanged(); }
@Test public void setSelection_changesSelection() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource secondCardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, secondCardSource); assertEquals(2, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver, times(1)).onChanged(); assertNull(mMaskedCardAdapter.getSelectedSource()); assertNotNull(secondCardSource); assertNotNull(secondCardSource.getId()); mMaskedCardAdapter.setSelectedSource(secondCardSource.getId()); verify(mAdapterDataObserver, times(2)).onChanged(); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(secondCardSource.getId(), mMaskedCardAdapter.getSelectedSource().getId()); }