MessageViewAdapter extends BaseAdapter { @Override public long getItemId(int position) { if (position >= items.size() || position < 0) { return -1; } return items.get(position).getMessageId().hashCode(); } MessageViewAdapter(@NonNull Context context, int layout); @Override int getCount(); @Nullable @Override Object getItem(int position); @Override long getItemId(int position); @NonNull @Override View getView(int position, @Nullable View convertView, @Nullable ViewGroup parent); void set(@NonNull Collection<Message> collection); @Override boolean hasStableIds();  }
@Test public void testGetItemId() { assertEquals(-1, messageViewAdapter.getItemId(0)); assertEquals(-1, messageViewAdapter.getItemId(-1)); messageViewAdapter.set(Arrays.asList(MessageCenterTestUtils.createMessage("id-0", null, false), MessageCenterTestUtils.createMessage("id-1", null, false))); assertEquals("id-0".hashCode(), messageViewAdapter.getItemId(0)); assertEquals("id-1".hashCode(), messageViewAdapter.getItemId(1)); assertEquals(-1, messageViewAdapter.getItemId(2)); assertEquals(-1, messageViewAdapter.getItemId(-1)); }