
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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