有什么方法可以从Android中的onCreateLoader中的两个不同的游标加载器获得结果

时间:2015-09-30 05:50:34

标签: android cursor simplecursoradapter android-cursoradapter android-cursorloader

我正在尝试从手机的联系簿中获取号码和电子邮件地址。 对于数字,内容uri是ContactsContract.Commomdatakinds.Phone.CONTENT_URI 电子邮件是ContactsContract.Commomdatakinds.Email.CONTENT_URI 所以现在我想将这两个URI传递给游标加载器并从中获取结果,但问题是游标加载器一次只能返回一个游标。所以任何人都可以帮助我解决这个问题,代码如下所示

public class Cursor extends Fragment implements LoaderManager.LoaderCallbacks<android.database.Cursor>
{
    ListView listView;
    ArrayList<String> contact_id;
    int position=0;
    private static final int URL_LOADER = 0;

    private static final int NAME_URL = 1;
    private static final int NUMBER_URL = 2;
    private static final int EMAIL_URL = 3;

    public String[] mFromColumns = {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Email.ADDRESS


    };

    public int[] mToFields = {
            R.id.text1,
            R.id.text2,
            R.id.text3,
            R.id.text4
    };

    SimpleCursorAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View v = inflater.inflate(R.layout.cursor,container,false);


        getLoaderManager().initLoader(NUMBER_URL, null, this);
        getLoaderManager().initLoader(EMAIL_URL, null, this);

        listView = (ListView) v.findViewById(R.id.list_item);


        mAdapter =
                new SimpleCursorAdapter(
                        getContext(),                // Current context
                        R.layout.textview,  // Layout for a single row
                        null,                // No Cursor yet
                        mFromColumns,        // Cursor columns to use
                        mToFields,           // Layout fields to use
                        0                    // No flags
                );

// Sets the adapter for the view
        listView.setAdapter(mAdapter);


        return v;
    }


    @Override
    public Loader<android.database.Cursor> onCreateLoader(int i, Bundle bundle) {

            switch (i) {

                case NUMBER_URL:
                    Log.i("URL_LOADER","LOADING DATA");
                    // Returns a new CursorLoader
                    return new CursorLoader(
                            getActivity(),   // Parent activity context
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,// Table to query
                            null,     // Projection to return
                            null,            // No selection clause
                            null,            // No selection arguments
                            null             // Default sort order
                    );
                case EMAIL_URL:
                    Log.i("URL_LOADER","LOADING DATA");
                    // Returns a new CursorLoader
                    return new CursorLoader(
                            getActivity(),   // Parent activity context
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,        // Table to query
                            null,     // Projection to return
                            null,            // No selection clause
                            null,            // No selection arguments
                            null             // Default sort order
                    );
                default:
                    Log.i("URL_LOADER","Not Loading");
                    // An invalid id was passed in
                    return null;

            }
    }

    @Override
    public void onLoadFinished(Loader<android.database.Cursor> loader, android.database.Cursor cursor) {



        mAdapter.swapCursor(cursor);


    }

    @Override
    public void onLoaderReset(Loader<android.database.Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}

0 个答案:

没有答案