Java tutorial
/* * This code is part of "Asynchronous Android Programming". * * Copyright (C) 2016 Helder Vasconcelos (heldervasc@bearstouch.com) * Copyright (C) 2016 Packt Publishing * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * */ package com.packpublishing.asynchronousandroid.chapter4; import android.database.Cursor; import android.os.Bundle; import android.provider.BaseColumns; import android.provider.ContactsContract; import android.support.v4.app.FragmentActivity; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; public class PhoneListActivityRV extends FragmentActivity implements android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor> { public static final int PHONE_LIST_LOADER = "phone_list".hashCode(); private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.phone_list_layout_rv); mRecyclerView = (RecyclerView) findViewById(R.id.phone_list); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); getSupportLoaderManager().initLoader(PHONE_LIST_LOADER, null, PhoneListActivityRV.this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] columns = new String[] { BaseColumns._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; return new CursorLoader(this, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, columns, // projection null, // selection null, // selectionArgs null // sortOrder ); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter = new ContactListAdapter(data); mRecyclerView.setAdapter(mAdapter); } @Override public void onLoaderReset(Loader<Cursor> loader) { mRecyclerView.setAdapter(mAdapter); } }