android contacts data

来源:互联网 发布:百度移动优化排名技术 编辑:程序博客网 时间:2024/05/17 01:21

content provider

Demo是给的一个联系人的栗子:点击打开链接

part1

contacts:

 A CursorLoader runs its query on a thread that's separate from the UI thread. 

首先需要权限:READ_CONTACTS

ContactsContract don't have to define your own constants for content URIs, table names, or columns

uses a CursorLoader to retrieve data from the provider, you must specify that itimplements the loader interface LoaderManager.LoaderCallbacks.

 Contacts.DISPLAY_NAME_PRIMARY requires Android 3.0 ,in versions previous to that, its name is Contacts.DISPLAY_NAME.

 Contacts._ID and LOOKUP_KEY are used together to construct a content URI for the contact the user selects.

Using "?" as a placeholder ensures that the search specification is generated by binding rather than by SQL compilation. so eliminates the possibility of malicious SQL injection. 


use a CursorLoader to retrieve data from the Contacts Provider. 主要是实现LoaderCallbacks接口

using a CursorLoader to retrieve data, you must initialize the background thread and other variables that control asynchronous retrieval.开的是异步线程。


public class ContactsFragment extends Fragment implements

        LoaderManager.LoaderCallbacks<Cursor> {

    ...

    // Called just before the Fragment displays its UI

    @Override

    public void onActivityCreated(Bundle savedInstanceState) {

        // Always call the super method first

        super.onActivityCreated(savedInstanceState);

        ...

        // Initializes the loader

        getLoaderManager().initLoader(0, null, this);

    }

同时实现接口函数onCreateLoader(), which is called by the loader framework immediately after you call initLoader().In onCreateLoader(), set up the search string pattern. 主要是返回一个CursorLoader。为了将字符串改为pattern,插入%表示0或多个字符,_表示一个字符,To make a string into a pattern, insert "%" (percent) characters to represent a sequence of zero or more characters, or "_" (underscore) characters to represent a single character, or both. For example, the pattern "%Jefferson%" would match both "Thomas Jefferson" and "Jefferson Davis".

@Override

    public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {

        /*

         * Makes search string into pattern and

         * stores it in the selection array

         */

        mSelectionArgs[0] = "%" + mSearchString + "%";

        // Starts the query

        return new CursorLoader(

                getActivity(),

                Contacts.CONTENT_URI,

                PROJECTION,

                SELECTION,

                mSelectionArgs,

                null

        );

    }

实现onLoadFinished,将Cursor的结果显示在list view上,调用swapCursor

The loader framework calls onLoadFinished() when the Contacts Provider returns the results of the query. In this method, put the result Cursor in the SimpleCursorAdapter. This automatically updates the ListView with the search results:

    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

        // Put the result Cursor in the adapter for the ListView

        mCursorAdapter.swapCursor(cursor);

    }

实现接口中的onLoaderReset方法,如果接收到stale数据,则删除adapter指向当前cursor的引用,否则会内存泄露。

The method onLoaderReset() is invoked when the loader framework detects that the result Cursor contains stale data. Delete the SimpleCursorAdapter reference to the existing Cursor. If you don't, the loader framework will not recycle the Cursor, which causes a memory leak. For example:

    @Override

    public void onLoaderReset(Loader<Cursor> loader) {

        // Delete the reference to the existing Cursor

        mCursorAdapter.swapCursor(null);这里的adapter是CursorAdapter类型的变量

    }

}



part2

显示contacts数据时,使用一个fragment,首先Initialize the Fragment. Add the empty, public constructor required by the Android system, and inflate the Fragment object's UI in the callback method onCreateView().


// Empty public constructor, required by the system

    public ContactsFragment() {}


    // A UI Fragment must inflate its View

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {

        // Inflate the fragment layout

        return inflater.inflate(R.layout.contact_list_fragment, container, false);

    }


点击事件,获取activity中的cursor,并且移动显示

@Override

    public void onItemClick(

        AdapterView<?> parent, View item, int position, long rowID) {

        // Get the Cursor

        Cursor cursor = parent.getAdapter().getCursor();

        // Move to the selected contact

        cursor.moveToPosition(position);

        // Get the _ID value

        mContactId = getLong(CONTACT_ID_INDEX);

        // Get the selected LOOKUP KEY

        mContactKey = getString(CONTACT_KEY_INDEX);

        // Create the contact's content Uri

        mContactUri = Contacts.getLookupUri(mContactId, mContactKey);

        /*

         * You can use mContactUri as the content URI for retrieving

         * the details for a contact.

         */

    }


在activity里,创建时

public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        ...

        // Gets the ListView from the View list of the parent activity

        mContactsList =

            (ListView) getActivity().findViewById(R.layout.contact_list_view);

        // Gets a CursorAdapter

        mCursorAdapter = new SimpleCursorAdapter(

                getActivity(),

                R.layout.contact_list_item,

                null,

                FROM_COLUMNS, TO_IDS,

                0);

        // Sets the adapter for the ListView

        mContactsList.setAdapter(mCursorAdapter);

    }


总结

To implement this type of retrieval, first implement the following code, as listed in previous sections:

  • Request Permission to Read the Provider.
  • Define ListView and item layouts.
  • Define a Fragment that displays the list of contacts.
  • Define global variables.
  • Initialize the Fragment.
  • Set up the CursorAdapter for the ListView.
  • Set the selected contact listener.
  • Define constants for the Cursor column indexes.Although you're retrieving data from a different table, the order of the columns in the projection is the same, so you can use the same indexes for the Cursor.
  • Define the onItemClick() method.
  • Initialize the loader.
  • Implement onLoadFinished() and onLoaderReset().
0 0
原创粉丝点击