How to Use Android loader?


This example demonstrate about How to Use Android loader

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent">ss
   <ListView
      android:id = "@+id/list"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_centerHorizontal = "true"
      android:layout_centerVertical = "true" />
</LinearLayout>

In the above code, we have taken listview to show contact names.

Step 3 − Add the following code to src/MainActivity.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.Manifest;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity implements android.app.LoaderManager.LoaderCallbacks<Cursor> {
   private static final int LOADER_CONTACTS = 100;
   private static final int PERMISSION_CONTACTS = 101;
   private static final String[] PROJECTION = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
   ListView lstContact;
   SimpleCursorAdapter adapter;
   String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
   int[] to = {android.R.id.text1};
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, 0);
      ListView listView = (ListView) findViewById(R.id.list);
      listView.setAdapter(adapter);
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) ! = PackageManager.PERMISSION_GRANTED) {
         ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_CONTACTS);
      } else {
         getLoaderManager().initLoader(LOADER_CONTACTS, null,this);
      }
   }
   @Override
   public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
      if (id == LOADER_CONTACTS) {
         return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI, PROJECTION, null, null, null);
      } else {
         return null;
      }
   }
   @Override
   public void onLoadFinished(android.content.Loader<Cursor> loader, Cursor data) {
      adapter.swapCursor(data);
      adapter.notifyDataSetChanged();
   }
   @Override
   public void onLoaderReset(android.content.Loader<Cursor> loader) {
      adapter.swapCursor(null);
   }
}

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

Click here to download the project code

Updated on: 30-Jul-2019

876 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements