Loader之二:CursorLoader基本实例
来源:互联网 发布:通达信交易软件 编辑:程序博客网 时间:2024/06/14 20:16
参考APIDEMO:sdk\samples\android-19\content\LoaderCursor
1、创建主布局文件,里面只包含一个Fragment。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/FrameLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <fragment android:id="@+id/fragment_list" android:name="com.example.android.content.loadercursor.CursorLoaderListFragment" android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>
2、创建主Activity文件中的android:name加载相应的Fragment
package com.example.android.content.loadercursor;import android.app.Activity;import android.app.ListFragment;import android.app.LoaderManager;import android.database.Cursor;import android.os.Bundle;import android.widget.SearchView;/** * The entry point to the CursorLoader sample. This sample demonstrates the use * of the {@link LoaderManager} to retrieve data from a {@link Cursor}. Here, a * list of contacts is displayed in a {@link ListFragment} and filtered using a * {@link SearchView} ActionBar item. */public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
3、创建相应的Fragment
package com.example.android.content.loadercursor;import android.app.ListFragment;import android.app.LoaderManager;import android.content.CursorLoader;import android.content.Loader;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.provider.ContactsContract.Contacts;import android.view.View;import android.widget.ListView;import android.widget.SearchView;import android.widget.SimpleCursorAdapter;import android.widget.Toast;/** * A {@link ListFragment} that shows the use of a {@link LoaderManager} to * display a list of contacts accessed through a {@link Cursor}. *//* * (1)继承Fragment或者其子类,用于创建一个Fragment。实现LoaderManager.LoaderCallbacks接口,用于与Loader的交互 * 。 官方文档: A callback interface for a client to interact with the LoaderManager. * For example, you use the onCreateLoader() callback method to create a new * loader. */public class CursorLoaderListFragment extends ListFragment implementsLoaderManager.LoaderCallbacks<Cursor> {// This is the Adapter being used to display the list's data.SimpleCursorAdapter mAdapter;// The SearchView for doing filtering.SearchView mSearchView;/* * (2)在Activity被创建时调用此方法。Called when the fragment's activity has been * created and this fragment's view hierarchy instantiated. You typically * initialize a Loader within the activity's onCreate() method, or within * the fragment's onActivityCreated() method. */@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);// Give some text to display if there is no data. In a real// application this would come from a resource.setEmptyText("No phone numbers");/* * Create an empty adapter we will use to display the loaded data. The * simple_list_item_2 layout contains two rows on top of each other * (text1 and text2) that will show the contact's name and status. */// (3)设置Fragment的顯示內容mAdapter = new SimpleCursorAdapter(getActivity(),android.R.layout.simple_list_item_2, null, new String[] {Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },new int[] { android.R.id.text1, android.R.id.text2 }, 0);setListAdapter(mAdapter);// Start out with a progress indicator.setListShown(false);// Prepare the loader. Either re-connect with an existing one,// or start a new one./* * (4)創建一個Loader,此Loader用于為Fragment載入內容。You typically initialize a * Loader within the activity's onCreate() method, or within the * fragment's onActivityCreated() method. * 此方法將自動調用LoaderManager.LoaderCallbacks接口的onCreateLoader方法。 */getLoaderManager().initLoader(0, null, this);}/** * An item has been clicked in the {@link ListView}. Display a toast with * the tapped item's id. */@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {Toast.makeText(getActivity(), "Item clicked: " + id, Toast.LENGTH_LONG).show();}// These are the Contacts rows that we will retrieve.static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {Contacts._ID, Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS,Contacts.LOOKUP_KEY, };/* * (5)Loader被創建時的操作,一般用于加載內容。In this example, the onCreateLoader() callback * method creates a CursorLoader. You must build the CursorLoader using its * constructor method, which requires the complete set of information needed * to perform a query to the ContentProvider. */public Loader<Cursor> onCreateLoader(int id, Bundle args) {// This is called when a new Loader needs to be created. This// sample only has one Loader, so we don't care about the ID.// First, pick the base URI to use depending on whether we are// currently filtering.Uri baseUri;baseUri = Contacts.CONTENT_URI;// Now create and return a CursorLoader that will take care of// creating a Cursor for the data being displayed.String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("+ Contacts.DISPLAY_NAME + " != '' ))";String order = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";return new CursorLoader(getActivity(), baseUri,CONTACTS_SUMMARY_PROJECTION, select, null, order);}/* * (6)内容被加载完成后的操作。The loader will release the data once it knows the * application is no longer using it. For example, if the data is a cursor * from a CursorLoader, you should not call close() on it yourself. If the * cursor is being placed in a CursorAdapter, you should use the * swapCursor() method so that the old Cursor is not closed. */public void onLoadFinished(Loader<Cursor> loader, Cursor data) {// Swap the new cursor in. (The framework will take care of closing the// old cursor once we return.)// Swap in a new Cursor, returning the old Cursor. Unlike// changeCursor(Cursor), the returned old Cursor is not closed.mAdapter.swapCursor(data);// The list should now be shown.if (isResumed()) {setListShown(true);} else {setListShownNoAnimation(true);}}// (7)Loader被重新加载时的操作。public void onLoaderReset(Loader<Cursor> loader) {// This is called when the last Cursor provided to onLoadFinished()// above is about to be closed. We need to make sure we are no// longer using it.mAdapter.swapCursor(null);}}
- Loader之二:CursorLoader基本实例
- Loader之二:CursorLoader基本实例
- Android Loader(二) CursorLoader
- Loader,AsyncTaskLoader,CursorLoader,LoaderManager 相关
- Loader,AsyncTaskLoader,CursorLoader与LoaderManager
- Loader,AsyncTaskLoader,CursorLoader与LoaderManager
- Loader之CursorLoader的使用——加载系统短信的数据(1)
- Loader异步装载CursorLoader的案例
- Android Loader(三) 结合CursorLoader分析Loader相关源码
- CursorLoader
- CursorLoader
- CursorLoader
- Android的CursorLoader用法小结(Loader的间接子类)
- 深入源码解析Android中Loader、AsyncTaskLoader、CursorLoader、LoaderManager
- 深入理解Android中Loader、AsyncTaskLoader、CursorLoader、LoaderManager
- android的CursorLoader用法小结及实例
- Android CursorLoader实例详解(附源码)
- GUI的基本使用方式之代码实例(二)
- assert用法总结
- 旅游背包(多维有界的背包问题)
- 求先序遍历
- 使用 GDB 调试 Linux 软件
- 游戏开发中的数学和物理算法(5):圆(2D)和球(3D)
- Loader之二:CursorLoader基本实例
- 游戏开发中的数学和物理算法(6):圆和球的碰撞检测
- 一步一步学Yii开发(一)
- dedecms 5.7列表页重复标题和描述的解决方案
- 深入浅出之正则表达式
- 机房收费系统之 导出excel
- android应用开发之xml生成————————xml命名域与属性
- C#遍历局域网的几种方法及比较
- 【黑马程序员】java中的继承多态的体现,什么情况下用继承