Просмотр исходного кода

Add Loader example to display contacts information from the Contacts
provider.

Cristian Tanas 11 лет назад
Родитель
Сommit
2906e7f5b8

+ 29 - 0
ContentProviderContactsListWithLoader/AndroidManifest.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.uab.android.contentprovider.contactslistwithloader"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+    
+    <uses-permission android:name="android.permission.READ_CONTACTS"/>
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

BIN
ContentProviderContactsListWithLoader/ic_launcher-web.png


BIN
ContentProviderContactsListWithLoader/libs/android-support-v4.jar


BIN
ContentProviderContactsListWithLoader/res/drawable-hdpi/ic_launcher.png


BIN
ContentProviderContactsListWithLoader/res/drawable-mdpi/ic_launcher.png


BIN
ContentProviderContactsListWithLoader/res/drawable-xhdpi/ic_launcher.png


BIN
ContentProviderContactsListWithLoader/res/drawable-xxhdpi/ic_launcher.png


+ 11 - 0
ContentProviderContactsListWithLoader/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
ContentProviderContactsListWithLoader/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 7 - 0
ContentProviderContactsListWithLoader/res/values/strings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">ContentProviderContactsListWithLoader</string>
+    <string name="hello_world">Hello world!</string>
+
+</resources>

+ 20 - 0
ContentProviderContactsListWithLoader/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 74 - 0
ContentProviderContactsListWithLoader/src/org/uab/android/contentprovider/contactslistwithloader/MainActivity.java

@@ -0,0 +1,74 @@
+package org.uab.android.contentprovider.contactslistwithloader;
+
+import android.app.ListActivity;
+import android.app.LoaderManager.LoaderCallbacks;
+import android.content.CursorLoader;
+import android.content.Loader;
+import android.database.Cursor;
+import android.os.Bundle;
+import android.provider.ContactsContract.Contacts;
+import android.widget.SimpleCursorAdapter;
+
+public class MainActivity extends ListActivity {
+	
+	static final int CONTACTS_LOADER = 0;
+	
+	SimpleCursorAdapter cursorAdapter;
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		
+		// Create a SimpleCursorAdapter for the ListView passing a null cursor
+		this.cursorAdapter = new SimpleCursorAdapter(
+				this, 
+				android.R.layout.simple_list_item_1, 
+				null, 
+				new String[] { Contacts.DISPLAY_NAME }, 
+				new int[] { android.R.id.text1 }, 
+				0
+				);
+		
+		setListAdapter(this.cursorAdapter);
+		
+		// Prepare the Loader. Either re-connect with an existing one or start a new one
+		getLoaderManager().initLoader(
+				CONTACTS_LOADER, 		// Unique identifier for the Loader
+				null, 					// Optional arguments to supply to the loader at construction
+				loaderCallbacks			// Interface the LoaderManager will call to report about changes in the state of the Loader
+				);
+	}
+	
+	LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {
+
+		@Override
+		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+			// A new Loader has to be created.
+			// Return a CursorLoader that will take care of creating a Cursor for the 
+			// data being displayed.
+			CursorLoader cursorLoader = new CursorLoader(
+					MainActivity.this, 
+					Contacts.CONTENT_URI, 
+					new String[] { Contacts._ID, Contacts.DISPLAY_NAME }, 
+					null, 
+					null, 
+					Contacts.DISPLAY_NAME
+					);
+			
+			return cursorLoader;
+		}
+
+		@Override
+		public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+			
+			// Swap the new cursor in. The old cursor is automatically closed by the framework.
+			cursorAdapter.swapCursor(data);
+		}
+
+		@Override
+		public void onLoaderReset(Loader<Cursor> arg0) {
+			
+			cursorAdapter.swapCursor(null);
+		}
+	};
+}